I have a number with 7 decimals in SwuifUI:
@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?
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.