R Robert_Poland Member Licensed User Jun 28, 2020 #1 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
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 Jun 28, 2020 #2 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
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.
E emexes Expert Licensed User Longtime User Jun 29, 2020 #4 klaus said: It is not possible to display any double number exactly in digital. Click to expand... 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
klaus said: It is not possible to display any double number exactly in digital. Click to expand... 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.
E emexes Expert Licensed User Longtime User Jun 29, 2020 #5 Robert_Poland said: depend on displaying only two digits after the decimal point how to do it ? Click to expand... 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
Robert_Poland said: depend on displaying only two digits after the decimal point how to do it ? Click to expand... 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).