VB.Net and Rounding Numbers

Our Web Engineer Reports:

In Javascript you round like so:
Var num = “11.925”
Num.toFixed(2) will give you 11.93

In PHP you round like so:
echo round(num, 2) will give you 11.93

In VB.NET you round like so:
Dim num as Decimal = 11.925
Math.Round(num, 2) will give you 11.92….

WTF?
All microsoft’s built in round functions do “Bankers Rounding” it rounds to the nearest even number
Math.Round(11.925, 2) = 11.92
Math.Round(11.935,2) = 11.93

WTF !?!

Why is this important? Well when you're writing a quoting system or something that integrates with oh, say an accounting package, the rounding of MONEY is imporatant and should be accurate.

Bankers Rounding eh? So this is where my money is going at the bank.

Sheesh.

Technorati Tags: , , ,

Other intersting links on this rounding technique:

This MS Blog: Fabulous Adventures in Coding

Wikipedia: The round-to-even rounding rule 

3 thoughts on “VB.Net and Rounding Numbers

  1. With Excel formula:
    =ROUND(11.925,2) = 11.93

    WTF??

    With Excel VBA:
    round(11.925,2) = 11.92
    round(11.935,2) = 11.94

    With Word VBA:
    round(11.925,2) = 11.92
    round(11.935,2) = 11.94

    If VB.Net uses Bankers’ Rounding, shouldn’t
    Math.Round(11.935,2) = 11.94???

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

    This will get around it..

  3. Hi,

    In my VB .Net application , i want to get the amount value should be rounded off by nearest rupes. Eg: 78.89 will be 79.0.

    pls send me the code for this in vb.net

Leave a Reply