Re: Problems with Math.Round
- From: "William Stacey [C# MVP]" <william.stacey@xxxxxxxxx>
- Date: Tue, 16 Oct 2007 22:09:30 -0400
This is well treated in the docs. The default is "ToEven" rounding (AKA
Bankers rounding). This tries to prevent rounding errors by always rounding
in one direction. If the digit is mid-point, and the prior digit is an even
number, it rounds to even. If the number is odd, then rounds other way. The
kind of rounding you may have learned in school is always round up to next
digit - or away from zero.
decimal d1 = (decimal)12.985;
decimal d2 = Math.Round(d1, 2, MidpointRounding.ToEven);
decimal d3 = Math.Round(d1, 2, MidpointRounding.AwayFromZero);
decimal d4 = Math.Round((decimal)12.975, 2, MidpointRounding.ToEven);
Console.WriteLine("Value:{0} ToEven:{1} AwayFromZero:{2}
d4:{3}",d1,d2,d3,d4);
// Value:12.985 ToEven:12.98 AwayFromZero:12.99 d4-ToEven:12.98
--
William Stacey [C# MVP]
PowerLocker, PowerPad
www.powerlocker.com
"Rene" <Rene@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:45352A19-2E38-4A84-8524-992B0220BA90@xxxxxxxxxxxxxxxx
| Hello everyone
|
| I have a problem with Math.Round, it´s ocurring some strange:
|
| Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99
|
| Why?? What is the problem?
|
| Help ME !!!!
|
| Renato
.
- Follow-Ups:
- Re: Problems with Math.Round
- From: Benny Skjold Tordrup
- Re: Problems with Math.Round
- References:
- Problems with Math.Round
- From: Rene
- Problems with Math.Round
- Prev by Date: Problems with Math.Round
- Next by Date: Re: Display a Web Form then Do Something
- Previous by thread: Problems with Math.Round
- Next by thread: Re: Problems with Math.Round
- Index(es):
Relevant Pages
|
Loading