Results 1 to 6 of 6

Thread: Math.Round not rounding to correct amount

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    2

    Math.Round not rounding to correct amount

    Please refer to the following - does anyone know why this is not rounding up the second decimal place? The correct result shoudl be 2059.43.

    Dim tempVFD, amount, exch As Double

    amount = 2025
    exch = 1.017
    TempVFD = amount * exch ' returns 2059.4249999999997
    tempVFD = Round(tempVFD, 2) ' returns 2059.42
    MsgBox(test.ToString("0.00"))

    What am I missing?

    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Math.Round not rounding to correct amount

    Floating-point data types like Double and Single are not capable of accurately representing every value. If you change your data type to Decimal instead of Double then you'll be able to get the result you expect. Decimal is slower to work with than Double but is not as susceptible to such errors.

    That said, your code as is will still give you 2059.42 because, as the documentation clearly states, the default behaviour of Math.Round is to round 5 to the nearest even number. That's to prevent repeated roundings of 5 pushing values continually upwards. If you want 5 to be rounded up all the time then you have to use an overload of Round that takes a MidpointRounding value as an argument.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Math.Round not rounding to correct amount

    Also called: Bankers Rounding.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    2

    Re: Math.Round not rounding to correct amount

    Thanks for the responses - your terminology (MidPointRounding & Banker's Rounding) pointed me in the right direction. Apparently Mircosoft suggests that you use the following function to acheive "Symmetric arithmetic rounding" (rounds .5 away from 0):

    Function SymArith(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    SymArith = Fix(X * Factor + 0.5 * Sign(X)) / Factor
    ' Alternately:
    ' SymArith = Abs(AsymArith(X, Factor)) * Sgn(X)
    End Function

    I tried this function and I now get the desired results.

    Thanks again

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Math.Round not rounding to correct amount

    Quote Originally Posted by yvrmarc View Post
    Thanks for the responses - your terminology (MidPointRounding & Banker's Rounding) pointed me in the right direction. Apparently Mircosoft suggests that you use the following function to acheive "Symmetric arithmetic rounding" (rounds .5 away from 0):

    Function SymArith(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    SymArith = Fix(X * Factor + 0.5 * Sign(X)) / Factor
    ' Alternately:
    ' SymArith = Abs(AsymArith(X, Factor)) * Sgn(X)
    End Function

    I tried this function and I now get the desired results.

    Thanks again
    Um, they absolutely do not. Maybe prior to .NET 2.0 but that's over 8 years ago now. What you should do is what I told you that you should do back in the very first reply:
    If you want 5 to be rounded up all the time then you have to use an overload of Round that takes a MidpointRounding value as an argument.
    You don't have to change your code at all except to add an extra argument to you Math.Round call. Here's what you should have been reading in the first place:

    http://msdn.microsoft.com/en-us/libr...ath.round.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Math.Round not rounding to correct amount

    Code:
    tempVFD = Round(tempVFD, 2, MidpointRounding.AwayFromZero)

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