Android Question Mystery/bug: from string to number

wimpie3

Well-Known Member
Licensed User
Longtime User
Here is a little riddle for you. Given the following code, what does B4A return?

Code: Copy
Dim s As String
s="12.340"
Dim f As Float
f=s
Log(f)

You'd expect 12.34, right? The conversion from the string to the floating point numeric value should have dropped the last 0.

Well, it turns out the result that appears in the log is

Code: Copy
12.34000015258789

Some kind of rounding error? What's going on here and how can I get the "12.34" I want?
 
If you declare it as double, it will yield what you want. See example. I have seen a few posts discussing this with Erel's leadership:
Code: Copy
Dim s As String ="12.340"
Dim f As Double
f=s
Msgbox(f,"") 'displays 12.34
 
Upvote 0
Back
Top