Android Question [Solved] Problem with float variable

Pflichtfeld

Active Member
Licensed User
I fill a float-variable in a class with f.i. 1.6 (from a textedit-view)
this is the code, I use:
B4X:
Public Sub getKlingendicke As Float
    Log(mKlingenDicke)
    Return mKlingenDicke
End Sub

Public Sub setKlingendicke (dd As Float)
    mKlingenDicke=dd
End Sub
I do not understand what happens now: there are decimals altered, although I do not do any calculations with it. I add a screenshot to make that clear:

float.jpg

On the logs-window, you see the altered value. Hoovering over the value shows the exact 1.6
How can I cope with this problem?

Edit:
Round2 does not change the Situation, everything stayes as before.
B4X:
Public Sub getKlingendicke As Float
    Log(Round2(mKlingenDicke,2))
    Return Round2(mKlingenDicke,2)    'Seltsamerweise wird da immer was angehäng an den hinteren stellen.
End Sub
And also strange: When I assign this float-variable back to the text-property of the edittext.view (input type: dezimal_numbers), it is filled correctly with 1.6, ommitting the long decimals.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
This is characteristic of both Float and Double values. They are a binary representation of numbers and so cannot aways represent an exact decimal number. It is the same for all computer languages as it is a feature of the computer architecture. Rounding or Formatting the numbers for display usually achieves what you expect.

If you want exact representations see my BigNumbers library. https://www.b4x.com/android/forum/threads/bignumbers-library.9540/
 
Upvote 0
Top