Hi to all,
I'm trying to handling a decimal problem on a Double object.
After some computation on that double value, I got this kind of results:
Result 1 = 10.70
Result 2 = 5.50
Result 3 = 6.6999999999999999
My target is to obtain a number format like Result 1 and Result 2.
There is a object / internal IDE function that allow to set the decimals to 2 digits?
P.S.
After i post this question, I found this post:
Yes, 6.70 if I'm not mistaken. To get 6.69 you could do the following:
B4X:
Sub Truncate(value as Double, decimalPlaces As Int) As Double
Dim multiplier = Power(10, decimalPlaces) As Int
Dim result = value * multiplier As Int
Return (result / multiplier)
End Sub
'Usage:
Result3 = Truncate(69.999999999999, 2)
Don't modify the number.
When you want to show the number as a string then use:
B4X:
Label1.Text = $"$1.2{result}"$ 'if you want to show at most 2 fraction digits.
Label2.Text = NumberFormat2(result, 0, 2, 2, False) 'if you want to show exactly 2 fraction digits