R rafaelmotaquintana Active Member Licensed User Jul 16, 2017 #1 Does anybody know why this code returns 1019.99 ? B4X: Log("Res : " & (10.20 * 100))
Star-Dust Expert Licensed User Longtime User Jul 16, 2017 #2 Floating point calculations have a "rounding" that is not very accurate and therefore sometimes generates these errors. Upvote 0
Floating point calculations have a "rounding" that is not very accurate and therefore sometimes generates these errors.
R rafaelmotaquintana Active Member Licensed User Jul 16, 2017 #3 I see. I need to save the value 10.20, as 1020, but I always get 1019. Any code to do this correctly? Thanks Upvote 0
I see. I need to save the value 10.20, as 1020, but I always get 1019. Any code to do this correctly? Thanks
Star-Dust Expert Licensed User Longtime User Jul 16, 2017 #4 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: Jul 16, 2017 Upvote 0
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
MarcoRome Expert Licensed User Longtime User Jul 16, 2017 #5 B4X: Log("Res : " & Round(10.20 * 100)) Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Jul 17, 2017 #7 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
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
R rafaelmotaquintana Active Member Licensed User Jul 19, 2017 #8 Numberformat2 did it well . Thanks Upvote 0