May 19th, 2009, 09:35 AM
INT Function Rounding Problems
I have just spent the last few hours trying to find a problem where applying rounding after adding VAT was coming up with an odd few pence difference. Not that unusual you might think until I tripped over the code that was actually causing the porblem.
I have recreated the problem as an example as follows.....
Private Sub Command1_Click()
Dim TotVal As Currency
Dim VatRate As String
VatRate = "15"
TotVal = 2.1
MsgBox "Total VAT = " & ((TotVal * Val(VatRate)) + 0.5) / 100
MsgBox "Total VAT = " & Int((TotVal * Val(VatRate)) + 0.5) / 100
The first message box returns 0.32 as expected (2.1 * 15) = 31.5, +0.5 = 32, /100 = 0.32
However for some unknown reason the second message box using the INT function returns 0.31 !!! Basically according to this code the interger portion of 32 is 31.
Just to complicate matters, if I use a double instead of a currency variable then it returns the right result and if I use CINT again all is fine.
In my original program I am using a string for the VAT rate as it is stored in a flexgrid
I have found a work around by putting the VAT rate into a currency variable as well but I would like to know what is likely to be causing this kind of error.
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!