String to int conversion

sconlon

Active Member
Licensed User
Longtime User
In my project I am converting a euro string numeric, eg 2.55, to an int value in cents, eg 255, I do this by multiplying the string by 100 as below

B4X:
dim seuro as string
dim icents as int
icents = seuro*100

However I have noticed that the int value ends up as 254 rather than 255 so I expect there is some rounding happening. Is there some way to avoid this in the conversion?
 

klaus

Expert
Licensed User
Longtime User
The problem is the following:
Example:
Dim sVal As String
Dim dVal As Double
Dim iVal As Int
sVal = "2.55"
dVal = 2.55
dVal = dVal * 100 ' result 2.5499999999999997
iVal = dVal ' result 254

Try:
iVal = dVal * 100 + 0.00000000000001
Unfortunately only a workaround.

Best regards.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
All calculations are performed as Double floating point values. The actual result of 2.55*100 is 254.99999999999997 or thereabouts. It is being rounded down in the conversion to an integer value where the fractional part is discarded.

This is a classic problem in monetary calculations on computers as floating point values are approximations that cannot accurately represent every decimal value. You should use Doubles where possible but if even then the accuracy is not what you need then use BigDecimal from my BigNumbers library
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
I discovered an easier solution is just to remove the decimal point from the string and then set the int value to the resulting string. Only slight issue then is that I need to check that there are two decimal places in the string and if not I simply add one or two zeroes as required.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What about something like this:
B4X:
Dim seuro As String="2.55"
Dim icents As Int
icents = Round2(seuro*100,0)
Msgbox(icents,"")  'returns 255
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…