CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: INT Function Rounding Problems

1. Elite Member Power Poster
Join Date
Dec 2001
Posts
6,332

## Re: INT Function Rounding Problems

I believe the problem is actually two-fold. One is floating point error, making the result of the multiplication slightly off. The other is banker's rounding.

Try it like this, and the two give the same answer:
Code:
Private Sub Command1_Click()
Dim TotVal As Currency
Dim VatRate As String

VatRate = "15"

TotVal = 3.1

MsgBox "Total VAT = " & ((TotVal * Val(VatRate)) + 0.5) / 100
MsgBox "Total VAT = " & Int((TotVal * Val(VatRate)) + 0.5) / 100
End Sub
This demonstrates banker's rounding.

The FormatNumber() function does not use banker's rounding, but if you're dealing with financial data, that may not be appropriate. The Currency data type would be the way to go, along with using only numeric data types in mathematical formulas.

2. Junior Member
Join Date
Jul 2009
Posts
3

## Re: INT Function Rounding Problems

You want wierd try this very simple INT

Dim subtotal As Double
Dim vat As Double
subtotal = 12

vat = 180
vat = Int(vat)

vat = subtotal * 0.15
vat = 100 * vat
vat = Int(vat)

You will see that if you int the 180 it remains 180
But if you int 180 ont he line "vat=int(vat) then its 179.

Even more wierd try this: (and believe me this is impossible)

Dim subtotal As Double

subtotal = 19.99
subtotal = subtotal * 100
subtotal = Int(subtotal)

the result is 1998
however if you put any other number in (ie 19.98, 29.99, 99.99) then it gets the answer right the only one that goes wrong is 19.99.

3. Elite Member Power Poster
Join Date
Dec 2001
Posts
6,332

## Re: INT Function Rounding Problems

Yes uknod, the Double data type is subject to floating point error. Try it with the Currency data type, and you will see there is no such error.

4. Junior Member
Join Date
Jul 2009
Posts
3

## Re: INT Function Rounding Problems

I understand the floating point error, but why ONLY on 19.99 and not any other number?

5. Member
Join Date
Jul 2009
Location
Brazil
Posts
25

## Re: INT Function Rounding Problems

Originally Posted by uknod
I understand the floating point error, but why ONLY on 19.99 and not any other number?
Check this.

Code:
Dim x As Integer
subtotal = 19.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)
Stop
End Sub
or

Code:
Dim x As Currency
subtotal = 19.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)
Stop
End Sub
or

Code:
Dim x As Single
subtotal = 19.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)
Stop
End Sub
Last edited by Ash Katchup; July 28th, 2009 at 08:46 AM.

6. Junior Member
Join Date
Jul 2009
Posts
3

## Re: INT Function Rounding Problems

Yes but the question is why is 19.99 so different, to any other. Check this out.
Dim x As Double

subtotal = 29.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)

or

Dim x As Double

subtotal = 19.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)

only the 19.99 fails ANY other number is OK.

7. Member
Join Date
Jul 2009
Location
Brazil
Posts
25

## Re: INT Function Rounding Problems

Originally Posted by uknod
Yes but the question is why is 19.99 so different, to any other. Check this out.
Dim x As Double

subtotal = 29.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)

or

Dim x As Double

subtotal = 19.99
subtotal = subtotal * 100
x = subtotal
subtotal = Int(x)

only the 19.99 fails ANY other number is OK.
I can't tell you why, but i know that you can workaround using Single.

8. Elite Member Power Poster
Join Date
Dec 2001
Posts
6,332

## Re: INT Function Rounding Problems

Both Single and Double data types are subject to floating point errors.

9. Member
Join Date
Jul 2009
Location
Brazil
Posts
25

## Re: INT Function Rounding Problems

Originally Posted by WizBang
Both Single and Double data types are subject to floating point errors.
But int(csng(19.99*100)) = 1999
and int(cdbl(19.99*100)) = 1998

10. Elite Member Power Poster
Join Date
Dec 2001
Posts
6,332

## Re: INT Function Rounding Problems

Try it this way:
Int(CSng(19.99)*100)

11. Member
Join Date
Jul 2009
Location
Brazil
Posts
25

## Re: INT Function Rounding Problems

Originally Posted by WizBang
Try it this way:
Int(CSng(19.99)*100)
Hehehe, i missed that. Learning about VB bugs

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width