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
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.
Re: Math.Round not rounding to correct amount
Also called: Bankers Rounding.
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
Re: Math.Round not rounding to correct amount
Quote:
Originally Posted by
yvrmarc
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:
Quote:
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
Re: Math.Round not rounding to correct amount
Code:
tempVFD = Round(tempVFD, 2, MidpointRounding.AwayFromZero)