Android Question String to Float - inaccurate

doncx

Active Member
Licensed User
Longtime User
When I convert a string with several decimal positions to a float, the value gets changed.

There must be a simple reason, but it is escaping me.

The code below logs two lines
12.34567 (the string value)
12.345669746398926 (the resulting float value)

How do I accurately convert a decimal string to a float value? I've tried various formatting methods and continue to get this result.

B4X:
Dim str1 = "12.34567" As String
Dim num1 = 0 As Float
num1 = CNum(str1)
Log(str1)
Log(num1)

Sub CNum(str2cnvrt) As Float
    Return str2cnvrt
End Sub
 

Mahares

Expert
Licensed User
Longtime User
B4X:
Dim str1 = "12.34567" As String
    'You can use it as is since it is numeric:
    Log(str1*1.5)  'displays 18.518505
   
    'or you can use this:
    Dim num1 As Double=str1
    Log(num1)  'displays 12.34567
 
Upvote 0
Top