Android Question Weird multiplication error

Star-Dust

Expert
Licensed User
Longtime User
Floating point calculations have a "rounding" that is not very accurate and therefore sometimes generates these errors.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Dim V As Float = 10.20
Dim S As String = V
S=S.Replace(".","")
Log(s)
or
B4X:
Dim V As Float = 10.20
Dim S As String = V
Dim I As int = S.Replace(".","")
Log(i)
OR
B4X:
Sub Activity_Create
   Dim V As Float = 10.20
   Log(MakeInt(v))
End Sub

Sub MakeInt(N As String) As int
   Dim I As int
   I=N.Replace(".","")
   Return I
END Sub
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
also
B4X:
Log("Res : 1020")
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
NumberFormat / NumberFormat2 / smart string are the preferred methods to convert a number to string.

B4X:
Log($"Res : $1.0{10.20 * 100}"$) 'will print 1,020 ($1.0 means: minimum 1 integer, maximum 0 fractions)
Log($"Res : ${NumberFormat2(10.20 * 100, 1, 0, 0, False)}"$) 'will print 1020
 
Upvote 0
Top