S sconlon Active Member Licensed User Longtime User Apr 5, 2013 #1 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 Code: Copy 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?
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 Code: Copy 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 Apr 5, 2013 #2 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
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.
agraham Expert Licensed User Longtime User Apr 5, 2013 #3 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
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
S sconlon Active Member Licensed User Longtime User Apr 5, 2013 #4 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
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.
M Mahares Expert Licensed User Longtime User Apr 5, 2013 #5 What about something like this: Code: Copy Dim seuro As String="2.55" Dim icents As Int icents = Round2(seuro*100,0) Msgbox(icents,"") 'returns 255 Upvote 0
What about something like this: Code: Copy Dim seuro As String="2.55" Dim icents As Int icents = Round2(seuro*100,0) Msgbox(icents,"") 'returns 255
S sconlon Active Member Licensed User Longtime User Apr 5, 2013 #6 That seems to work with all the values I've tried so far. Thanks. Upvote 0