Re: Problems with Math.Round



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


.



Relevant Pages

  • Re: Rounding errors
    ... Take for example 10 random single digit numbers. ... if you take a set of large precision random numbers between ... The rounding to 2 digits will restore the full count 500,500. ... But it is not the _roundeing_ that is wrong, it is the truncation to ...
    (comp.lang.cobol)
  • Re: Using Float For Currency
    ... >> I have some methods that manipulate floats that represent a currency amount. ... round2 - unbiased rounding for floats ... The earlier article reported that the best way to round ... numbers is to always round up when the first digit dropped is 5. ...
    (comp.lang.ruby)
  • Re: Rounding errors
    ... No. Round to 3 then rounding to 2 is the beginner's mistake that gives ... What you consistently fail to notice is why the average of the 3 digit ... precision set it increases the average of that to 0.5005. ...
    (comp.lang.cobol)
  • Re: Rounding Numbers in queries
    ... supposed to reduce round-off error when summing large lists of numbers. ... you'll need to write your own Rounding routine. ... > digit rounded is a 5, if the digit to the left is odd it rounds up and if ... > digit to the left is even, it rounds down. ...
    (microsoft.public.access.queries)
  • Re: Rounding errors
    ... There are a VARIETY of rounding options. ... an upwardly compatible way) to COBOL. ... > collection of numbers in format v999 and we want to round them to v99. ... > digit of zero, a second containing 1-4 and a third containing 5-9. ...
    (comp.lang.cobol)

Loading