Bug? Bug when converting NumberFormat2 to Double

Filippo

Expert
Licensed User
Longtime User
Hi,

in B4a this code works without a problem, in B4i the app crashes.
Please fix the error.

B4X:
    Log("NumberFormat2=" & NumberFormat2(-0.0120000000000009, 1, 1, 1, False))
    Dim dbl As Double = NumberFormat2(-0.0120000000000009, 1, 1, 1, False)
    Log("dbl=" & dbl)
)

B4a-Log:
NumberFormat2=-0.0
dbl=-0

B4i-Log:
NumberFormat2=-0.0
Error occurred on line: 571 (Main)
Cannot parse: -0.0
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In most cases you should simply keep the number as-is. The rounding should be done when you show the number - at that point you convert it to a string with NumberFormat.

Double numbers cannot 100% represent decimal numbers anyway. You aren't really keeping a number with one decimal place (at least not in all cases).
 

emexes

Expert
Licensed User
Double numbers cannot 100% represent decimal numbers anyway.

Double numbers cannot 100% represent (most) decimal fractions anyway.

Binary fractions eg 1/2 1/4 1/8 1/16 etc eg 0.125 0.25 0.375 0.5 0.625 0.75 0.875 etc are no problem.

Non-binary fractions eg 1/3 1/5 1/6 1/7 1/9 1/10 etc can be a problem (but usually they're close enough to act and display as you'd expect).
 
Top