468,771 Members | 1,778 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Problems with Math.Round

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
Oct 17 '07 #1
4 10497
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" <Re**@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.com...
| Hello everyone
|
| I have a problem with Math.Round, its ocurring some strange:
|
| Math.Round(12.985) = 12.98, its wrong. It should be: 12.99
|
| Why?? What is the problem?
|
| Help ME !!!!
|
| Renato
Oct 17 '07 #2
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 {

/// <summary>
/// Rounds the double value to round to a specified number of decimals.
Does not use Bankers Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <param name="digits">The number of digits to round to</param>
/// <returns>The rounded value</returns>
public static double Round(double value, int digits) {
return System.Math.Round(value +
(System.Math.Sign(value)/System.Math.Pow(10, digits+1)), digits);
}

/// <summary>
/// Rounds the double value to round to 0 decimals. Does not use Bankers
Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <returns>The rounded value</returns>
public static double Round(double value) {
return Round(value, 0);
}

/// <summary>
/// Rounds the decimal value to round to a specified number of decimals.
Does not use Bankers Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <param name="digits">The number of digits to round to</param>
/// <returns>The rounded value</returns>
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);
}

/// <summary>
/// Rounds the decimal value to round to 0 decimals. Does not use Bankers
Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <returns>The rounded value</returns>
public static decimal Round(decimal value) {
return Round(value, 0);
}

}
"William Stacey [C# MVP]" <wi************@gmail.comskrev i en meddelelse
news:%2****************@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" <Re**@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.com...
| Hello everyone
|
| I have a problem with Math.Round, its ocurring some strange:
|
| Math.Round(12.985) = 12.98, its wrong. It should be: 12.99
|
| Why?? What is the problem?
|
| Help ME !!!!
|
| Renato


Oct 18 '07 #3
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" <Re**@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.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
Oct 18 '07 #4
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 {

/// <summary>
/// Rounds the double value to round to a specified number of decimals.
Does not use Bankers Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <param name="digits">The number of digits to round to</param>
/// <returns>The rounded value</returns>
public static double Round(double value, int digits) {
return System.Math.Round(value +
(System.Math.Sign(value)/System.Math.Pow(10, digits+1)), digits);
}

/// <summary>
/// Rounds the double value to round to 0 decimals. Does not use Bankers
Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <returns>The rounded value</returns>
public static double Round(double value) {
return Round(value, 0);
}

/// <summary>
/// Rounds the decimal value to round to a specified number of decimals.
Does not use Bankers Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <param name="digits">The number of digits to round to</param>
/// <returns>The rounded value</returns>
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);
}

/// <summary>
/// Rounds the decimal value to round to 0 decimals. Does not use Bankers
Rounding.
/// </summary>
/// <param name="value">The value to round</param>
/// <returns>The rounded value</returns>
public static decimal Round(decimal value) {
return Round(value, 0);
}

}
"William Stacey [C# MVP]" <wi************@gmail.comskrev i en meddelelse
news:%2****************@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" <Re**@discussions.microsoft.comwrote in message
news:45**********************************@microsof t.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


Oct 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by cody | last post: by
6 posts views Thread by ng_mr | last post: by
1 post views Thread by alyssa | last post: by
4 posts views Thread by Chris Davoli | last post: by
10 posts views Thread by David Coleman | last post: by
3 posts views Thread by Altman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.