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?
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.
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
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.