Android Question QUIZ - you can win... nothing ^_^

LucaMs

Expert
Licensed User
Longtime User
B4A, of course
B4X:
Dim D As Double = 1250.60
D = D * 100
Log("D = " & D)

What the result will be?


And this?:
B4X:
Dim D As Double = 1250.60
Dim L As Long = D * 100
Log("D = " & D)
Log("L =: " & L)



https://www.b4x.com/android/forum/threads/trasformare-un-importo-in-un-intero.58999/


B4X:
  Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      Dim D As Double = 1250.6
      D = D * 100
      Debug.Write(D)
      Application.Exit()
End Sub
Result: 125060
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
It is due to Java, not to B4A.
Note that you will get the right result with a float.
As D * 100 is rounded down when converted to a long, the wrong result is pretty logical for L.
If you want to control the precision, never use floats or doubles, only the BigDecimal class (in Java) or as I saw in real applications using currencies, convert everything to long for your computations and use floats or doubles only for the display (by dividing the integer value).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It is due to Java, not to B4A.
I added that line after, because below I posted the same code but in VB.Net (someone could not distinguish them: thanks @Erel ).


Note that you will get the right result with a float.
I did not know this and "instinctively" I would have thought (at least) the vice-versa.


As D * 100 is rounded down when converted to a long, the wrong result is pretty logical for L.
Right... pretty .
Note that using VB.Net, the resultS are "pretty right" anyway:
B4X:
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim D As Double = 1250.6
        Dim L As Long = D * 100
        D = D * 100
        Debug.WriteLine(D)
        Debug.WriteLine(L)
        Application.Exit()
    End Sub
125060
125060



This is the goal of the member who asked the question (Italian forum).



Thank you, @Informatix
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…