Precision bug

alander

Well-known member
Joined
Jun 26, 2007
Messages
120
Location
Singapore
Programming Experience
5-10
i did cint(99.5) and it becomes 99? aint it supposed to be 100?
 
I get 100.
 
uh.... u sure/ i googled and find that vb.net rounds down .5 =s

I am dealing with money here i need to get over this help pls
nvm got around this stupid bug
VB.NET:
    Private Function getAroundPrecisionBug(ByVal value As Decimal)
        Dim x As Decimal
        'get decimal value 
        Dim y As Decimal = value - Math.Floor(value)

        If y >= 0.5 Then
            x = Math.Ceiling(value)
        Else
            x = Math.Floor(value)
        End If

        Return x
    End Function
 
Last edited:
Bug? Cint uses the Math.Round method for rounding:
The integer nearest parameter d. If d is halfway between two integers, one of which is even and the other odd, then the even number is returned.
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding.
So CInt(99.5) is 100 and CInt(98.5) is 98, you see? Since it's "banker's rounding" it should be exactly what you need for your money.

There also exist an overloads of Math.Round where you can specify MidpointRounding method, for example:
VB.NET:
Dim i As Integer = Math.Round(98.5, MidpointRounding.AwayFromZero) '=99
 
Back
Top