Android Question Double and long

Robert_Poland

Member
Licensed User
Hi,
every time I press the button I change the value by 0.1 sometimes instead of 1.3 it displays e.g. the value 1.3000000000091
depend on displaying only two digits after the decimal point
how to do it ?
Thx

B4X:
Sub dodaj_Click

    Dim var As Double
    odejmij.Visible=True
    var = var + 0.1
     wynik.text=var &" °C"
    If var = 5 Then
    Dodaj.Visible=False
   
    End If
    End Sub
 

klaus

Expert
Licensed User
Longtime User
Instead of using
wynik.text=var &" °C"
use
wynik.text=NumberFormat2(var, 1, 2, 2, False) &" °C"
It is not possible to display any double number exactly in digital.
NumberFormat2 converts the number in a String with given conditions.
 
Upvote 0

emexes

Expert
Licensed User
It is not possible to display any double number exactly in digital.
More precisely: it is not possible to exactly represent any non-binary(-power) fraction in binary.

eg 0.625 (aka 5/8ths) can be represented exactly in both binary and decimal, and thus displays fine.
 
Upvote 0

emexes

Expert
Licensed User
depend on displaying only two digits after the decimal point
how to do it ?
Klaus' solution fixes your display issue, but be aware that the underlying variable might still be very (very very) slightly off.

Eg, if you increment it from 0 by 1, and then decrement it by 0.1 ten times, you might end up with a very small, or even negative, non-zero value. This can get super-exciting when you multiply it by eg 4095 to get a DAC output value.

Sometimes it is best to use scaled integers, eg if your number represented volts, then make your variable millivolts and thus step it by 100 millivolts (exactly) rather than 0.1 volts (approximately).
 
Upvote 0
Top