Problems with Math.Round

Problems with Math.Round

am 17.10.2007 02:34:00 von rene

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

Re: Problems with Math.Round

am 17.10.2007 04:09:30 von william.stacey

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" wrote in message
news:45352A19-2E38-4A84-8524-992B0220BA90@microsoft.com...
| 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

Re: Problems with Math.Round

am 18.10.2007 09:56:03 von Benny Skjold Tordrup

Please note, this is new to .NET 2.0.

In .NET 1.1, you need to role your own Round method like in this class:

public sealed class Math {

///


/// Rounds the double value to round to a specified number of decimals.
Does not use Bankers Rounding.
///

/// The value to round
/// The number of digits to round to
/// The rounded value
public static double Round(double value, int digits) {
return System.Math.Round(value +
(System.Math.Sign(value)/System.Math.Pow(10, digits+1)), digits);
}

///
/// Rounds the double value to round to 0 decimals. Does not use Bankers
Rounding.
///

/// The value to round
/// The rounded value
public static double Round(double value) {
return Round(value, 0);
}

///
/// Rounds the decimal value to round to a specified number of decimals.
Does not use Bankers Rounding.
///

/// The value to round
/// The number of digits to round to
/// The rounded value
public static decimal Round(decimal value, int digits) {
return System.Math.Round(value + Convert.ToDecimal(
System.Math.Sign(value)/System.Math.Pow(10, digits+1)), digits);
}

///
/// Rounds the decimal value to round to 0 decimals. Does not use Bankers
Rounding.
///

/// The value to round
/// The rounded value
public static decimal Round(decimal value) {
return Round(value, 0);
}

}


"William Stacey [C# MVP]" skrev i en meddelelse
news:%23WWWCLGEIHA.2268@TK2MSFTNGP02.phx.gbl...
> 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" wrote in message
> news:45352A19-2E38-4A84-8524-992B0220BA90@microsoft.com...
> | 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
>
>

Re: Problems with Math.Round

am 18.10.2007 18:35:01 von rene

Thanks William

You have right !! But I'm working .NET 1.1, is this the problem.


"William Stacey [C# MVP]" wrote:

> 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" wrote in message
> news:45352A19-2E38-4A84-8524-992B0220BA90@microsoft.com...
> | 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
>
>
>

Re: Problems with Math.Round

am 18.10.2007 18:40:01 von rene

Thanks Benny

I´m working .NET 1.1, your functions are OK.

Renato

"Benny Skjold Tordrup" wrote:

> Please note, this is new to .NET 2.0.
>
> In .NET 1.1, you need to role your own Round method like in this class:
>
> public sealed class Math {
>
> ///


> /// Rounds the double value to round to a specified number of decimals.
> Does not use Bankers Rounding.
> ///

> /// The value to round
> /// The number of digits to round to
> /// The rounded value
> public static double Round(double value, int digits) {
> return System.Math.Round(value +
> (System.Math.Sign(value)/System.Math.Pow(10, digits+1)), digits);
> }
>
> ///
> /// Rounds the double value to round to 0 decimals. Does not use Bankers
> Rounding.
> ///

> /// The value to round
> /// The rounded value
> public static double Round(double value) {
> return Round(value, 0);
> }
>
> ///
> /// Rounds the decimal value to round to a specified number of decimals.
> Does not use Bankers Rounding.
> ///

> /// The value to round
> /// The number of digits to round to
> /// The rounded value
> public static decimal Round(decimal value, int digits) {
> return System.Math.Round(value + Convert.ToDecimal(
> System.Math.Sign(value)/System.Math.Pow(10, digits+1)), digits);
> }
>
> ///
> /// Rounds the decimal value to round to 0 decimals. Does not use Bankers
> Rounding.
> ///

> /// The value to round
> /// The rounded value
> public static decimal Round(decimal value) {
> return Round(value, 0);
> }
>
> }
>
>
> "William Stacey [C# MVP]" skrev i en meddelelse
> news:%23WWWCLGEIHA.2268@TK2MSFTNGP02.phx.gbl...
> > 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" wrote in message
> > news:45352A19-2E38-4A84-8524-992B0220BA90@microsoft.com...
> > | 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
> >
> >
>
>
>