英文:
swiftui double with 7 decimals without rounding
问题
我在SwuifUI中有一个带有7位小数的数字:
```swift
@State var newLatitude : Double = 47.6364658
Text("\(newLatitude)") //这会显示47.636466
但是它会自动四舍五入为只有6位小数的47.636466
我该如何避免这种情况?
使用另一种数据类型吗?我仍然想使用Double,因为我有一个共享的Kotlin仓库。
而且我需要确切的7位小数,不要四舍五入,因为这是一个地理位置点。
我从Kotlin共享仓库获取数据的方式:
func getDoubleFromKotlin() {
Task{
do{
try await repo.getData().watch(block:{myValue in
self.newLatitude = myValue.latitude as Double//应该使用什么数据类型?
})
}
}
}
newLatitude
应该使用什么数据类型,才不会被自动四舍五入并且保留真实的7位小数值?
<details>
<summary>英文:</summary>
I have a number with 7 decimals in SwuifUI:
```swift
@State var newLatitude : Double = 47.6364658
Text("\(newLatitude)") //this displays 47.636466
But this will round automatically to 47.636466 that only has 6 decimals
How can I avoid this?
Use another data type? I still want to use the Double because I have a shared Kotlin repo.
Also I need the exact 7 decimals without rounding because this is a geolocation point.
The way that i get the data from kotlin shared repo:
func getDoubleFromKotlin() {
Task{
do{
try await repo.getData().watch(block:{myValue in
self.newLatitude = myValue.latitude as Double// what data type should be?
})
}
}
}
What type new latitude should have to not be automatically rounded and lose the real 7 decimal value?
答案1
得分: 3
为了回答原始问题,你只需正确格式化你的数值。在SwiftUI中,你可以直接在TextField
中这样做:
Text(newLatitude.formatted(.number.precision(.fractionLength(2...))))
开放范围,2...
,意味着它将使用至少2位小数数字,最多与数值包含的位数一样多。
这适用于Double
,但你也可以使用Decimal
。
英文:
To answer the original question, all you need to do is to properly format your value. In SwiftUI you can do it directly in the TextField
Text(newLatitude.formatted(.number.precision(.fractionLength(2...))))
The open ended range, 2...
, will mean it will use at least 2 decimal digits and up to as many as the value contains.
This works fine with Double
but you can also use Decimal
答案2
得分: 1
我建议使用Decimal128,这是Realm在Swift和Kotlin中都支持的数据类型。
它非常强大 - 甚至可以存储和处理货币;它类似于NSDecimalNumber。以下是一些Swift示例:
var decNum = Decimal128(value: 47.6364658) // 将decimal转换为Decimal128
var stringNum = Decimal128(string: "47.6364658") // 将字符串转换为Decimal128
var intNum = Decimal128(integerLiteral: 44) // 将整数转换为Decimal128
甚至可以从NSNumber中创建:
let num = NSDecimalNumber(decimal: 47.6364658)
var x = Decimal128(number: num)
您还可以轻松获取其数量级,这在计算距离时很方便:
let m = num.magnitude
将其作为字符串以输出到UI:
let s = dec128Num.stringValue
print(s)
47.6364658
如果需要在Swift中格式化数字,请使用NSNumberFormatter,它提供了许多关于有效数字等方面的灵活性。我不清楚Kotlin中的等效方法是什么。
英文:
I would suggest using Decimal128, which is a Realm supported type in both Swift as well as Kotlin.
It's quite powerful - you can even store and work with currency; it's similar to NSDecimalNumber. Here's some Swift examples:
var decNum = Decimal128(value: 47.6364658) //decimal to Decimal128
var stringNum = Decimal128(string: "47.6364658") //string to Decimal128
var intNum = Decimal128(integerLiteral: 44) //int to Decimal128
or even from an NSNumber
let num = NSDecimalNumber(decimal: 47.6364658)
var x = Decimal128(number: num)
You can also easily get the magnitude, which is handy when calculating distances
let m = num.magnitude
Get it as a string for outputting to UI
let s = dec128Num.stringValue
print(s)
47.6364658
If you need to format the number in Swift, please use NSNumberFormatter which provides a lot of flexibility for significant digits etc. I don't know what the Kotlin equivalent is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论