Mitchell,
I use Math.Round & Decimal.Round most of the time.
..NET 2.0 (VS.NET 2005, due out late in 2005) has overloaded Math.Round to
account for other rounding methods (mid point up or even/bankers).
http://msdn2.microsoft.com/library/m...us,vs.80).aspx
As suggested in the following previous post of mine, I find banker's
rounding (the current Math.Round function) to be the "logical" choice.
---x--- cut here ---x---
The way I understand it is:
Rather then always rounding the mid point up, "half of the time" you are
rounding it up & "half of the time" you are rounding it down.
The two "half of the time" together will average themselves out of the
picture.
When you always rounding the mid point up, then you start accumulating all
the fractions, rather then average them out...
The following demonstrates this:
Public Shared Sub Main()
Dim value1 As Decimal = 1.235D
Dim value2 As Decimal = 1.245D
Dim value3 As Decimal = value1 + value2
Dim value4 As Decimal = Decimal.Round(v alue1, 2) +
Decimal.Round(v alue2, 2)
Dim value5 As Decimal = RoundUp(value1, 2) + RoundUp(value2, 2)
Debug.WriteLine (value1, "value1")
Debug.WriteLine (value2, "value2")
Debug.WriteLine (value3, "value3")
Debug.WriteLine (value4, "value4")
Debug.WriteLine (value5, "value5")
End Sub
' may not handle negative value correctly...
Private Shared Function RoundUp(ByVal value As Decimal, ByVal decimals
As Integer) As Decimal
decimals = CInt(10 ^ decimals)
value *= decimals
value = Decimal.Truncat e(value + 0.5D)
value /= decimals
Return value
End Function
Notice how value4 & value5 are off by 1, if you are lot of rounding, this
can add up significantly. When you use banking rounding, this difference
will not add up as quickly or as much...
---x--- cut here ---x---
| I like the overloading idea - do you have any examples that I might go by?
NOTE: You need C# 2002 or higher or VB 2005 or higher:
http://msdn.microsoft.com/library/de...verloading.asp http://msdn.microsoft.com/library/de...ml/vs03h12.asp http://www.panopticoncentral.net/arc...7/01/1338.aspx
Hope this helps
Jay
"Mitchell Vincent" <mv******@newsg roup.nospam> wrote in message
news:eC******** ******@TK2MSFTN GP09.phx.gbl...
| Jay B. Harlow [MVP - Outlook] wrote:
| > Mitchell,
| > I normally do all my "currency math" with System.Decimal, of course when
I
| > use multiplication or division I need to be certain to round the numbers
| > "correctly" .
|
| How do you handle it exactly?
|
| I've always stored my money values as ints instead of fooling with the
| decimal game but now that is more of a problem than a help.
|
| I like the overloading idea - do you have any examples that I might go by?
|
| Thanks a million!
|
| --
| - Mitchell Vincent
| - kBilling - Invoices Made Easy!
| -
http://www.k-billing.com