Hello everyone
I have a problem with Math.Round, itÂ´s ocurring some strange:
Math.Round(12.9 85) = 12.98, itÂ´s wrong. It should be: 12.99
Why?? What is the problem?
Help ME !!!!
Renato 4 10880
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 midpoint, 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, MidpointRoundin g.ToEven);
decimal d3 = Math.Round(d1, 2, MidpointRoundin g.AwayFromZero) ;
decimal d4 = Math.Round((dec imal)12.975, 2, MidpointRoundin g.ToEven);
Console.WriteLi ne("Value:{0} ToEven:{1} AwayFromZero:{2 }
d4:{3}",d1,d2,d 3,d4);
// Value:12.985 ToEven:12.98 AwayFromZero:12 .99 d4ToEven:12.98

William Stacey [C# MVP]
PowerLocker, PowerPad www.powerlocker.com
"Rene" <Re**@discussio ns.microsoft.co mwrote in message
news:45******** *************** ***********@mic rosoft.com...
 Hello everyone

 I have a problem with Math.Round, it´s ocurring some strange:

 Math.Round(12.9 85) = 12.98, it´s wrong. It should be: 12.99

 Why?? What is the problem?

 Help ME !!!!

 Renato
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">Th e value to round</param>
/// <param name="digits">T he number of digits to round to</param>
/// <returns>The rounded value</returns>
public static double Round(double value, int digits) {
return System.Math.Rou nd(value +
(System.Math.Si gn(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">Th e 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">Th e value to round</param>
/// <param name="digits">T he number of digits to round to</param>
/// <returns>The rounded value</returns>
public static decimal Round(decimal value, int digits) {
return System.Math.Rou nd(value + Convert.ToDecim al(
System.Math.Sig n(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">Th e 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******** ********@TK2MSF TNGP02.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 midpoint, 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, MidpointRoundin g.ToEven);
decimal d3 = Math.Round(d1, 2, MidpointRoundin g.AwayFromZero) ;
decimal d4 = Math.Round((dec imal)12.975, 2, MidpointRoundin g.ToEven);
Console.WriteLi ne("Value:{0} ToEven:{1} AwayFromZero:{2 }
d4:{3}",d1,d2,d 3,d4);
// Value:12.985 ToEven:12.98 AwayFromZero:12 .99 d4ToEven:12.98

William Stacey [C# MVP]
PowerLocker, PowerPad www.powerlocker.com
"Rene" <Re**@discussio ns.microsoft.co mwrote in message
news:45******** *************** ***********@mic rosoft.com...
 Hello everyone

 I have a problem with Math.Round, it´s ocurring some strange:

 Math.Round(12.9 85) = 12.98, it´s wrong. It should be: 12.99

 Why?? What is the problem?

 Help ME !!!!

 Renato
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 midpoint, 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, MidpointRoundin g.ToEven);
decimal d3 = Math.Round(d1, 2, MidpointRoundin g.AwayFromZero) ;
decimal d4 = Math.Round((dec imal)12.975, 2, MidpointRoundin g.ToEven);
Console.WriteLi ne("Value:{0} ToEven:{1} AwayFromZero:{2 }
d4:{3}",d1,d2,d 3,d4);
// Value:12.985 ToEven:12.98 AwayFromZero:12 .99 d4ToEven:12.98

William Stacey [C# MVP]
PowerLocker, PowerPad www.powerlocker.com
"Rene" <Re**@discussio ns.microsoft.co mwrote in message
news:45******** *************** ***********@mic rosoft.com...
 Hello everyone

 I have a problem with Math.Round, itÂ´s ocurring some strange:

 Math.Round(12.9 85) = 12.98, itÂ´s wrong. It should be: 12.99

 Why?? What is the problem?

 Help ME !!!!

 Renato
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">Th e value to round</param>
/// <param name="digits">T he number of digits to round to</param>
/// <returns>The rounded value</returns>
public static double Round(double value, int digits) {
return System.Math.Rou nd(value +
(System.Math.Si gn(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">Th e 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">Th e value to round</param>
/// <param name="digits">T he number of digits to round to</param>
/// <returns>The rounded value</returns>
public static decimal Round(decimal value, int digits) {
return System.Math.Rou nd(value + Convert.ToDecim al(
System.Math.Sig n(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">Th e 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******** ********@TK2MSF TNGP02.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 midpoint, 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, MidpointRoundin g.ToEven);
decimal d3 = Math.Round(d1, 2, MidpointRoundin g.AwayFromZero) ;
decimal d4 = Math.Round((dec imal)12.975, 2, MidpointRoundin g.ToEven);
Console.WriteLi ne("Value:{0} ToEven:{1} AwayFromZero:{2 }
d4:{3}",d1,d2,d 3,d4);
// Value:12.985 ToEven:12.98 AwayFromZero:12 .99 d4ToEven:12.98

William Stacey [C# MVP]
PowerLocker, PowerPad www.powerlocker.com
"Rene" <Re**@discussio ns.microsoft.co mwrote in message
news:45******** *************** ***********@mic rosoft.com...
 Hello everyone

 I have a problem with Math.Round, itÂ´s ocurring some strange:

 Math.Round(12.9 85) = 12.98, itÂ´s wrong. It should be: 12.99

 Why?? What is the problem?

 Help ME !!!!

 Renato
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics 
by: cody 
last post by:
the documentation states that Math.Round supports banker's rounding but it
also states that if the last number is 5 it will rounded up if the whole
number is even, otherwise rounded down which is totally bullshit!
i expect:
Round(1.344, 2) == 123.34
Round(1.345, 2) == 123.35
and nothing else!
for a workaround i had to write my own...

by: ng_mr 
last post by:
No, not a question about "banker's rounding" or whatever it's called.
I want to round a double to the nearest 100th, so I perform the
following:
// original is a double
double result = Math.Round( original, 2 );
But I'm not happy with the results. E.g. if original is

by: alyssa 
last post by:
Hi guys,
Does C#.NET support Math.round? I get an error msg saying that System.Math
does not contain the a definition for 'round'.
So what can i do to make my calculation round to 2nd decimal point?
here are my code..
> start_time = Math.round (((call_start/100) * 60)*60);
Please correct my mistakes..
thanks in advance...

by: Chris Davoli 
last post by:
The folllowing will round to 526, but it should round to 527. It works
correctly for all other numbers, except for this one. Does anybody know of a
bug in Math.Round?
Dim ldecWater As Decimal = 526.5
CType(Math.Round(ldecWater, 0), String)

Chris Davoli

by: David Coleman 
last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
 
by: Altman 
last post by:
OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the problem but it
didn't. I have VS 2005 and I am using vb.net try rounding the number
4.935 to 2 decimals:
What I am seeing is the following:...

by: Zeng 
last post by:
Math.Round has good behavior as following:
Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7' is odd
However, if format.NumberDecimalDigits is 1
decimal d = 3.45M;
d.ToString( "F", format ); //Return 3.5  this is different from...

by: DaiOz 
last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please,
The code I have so far is below but I don't think its right please help as I need to complete this part to move onto the next.
<HTML>
<HEAD>
<TITLE>M150 TMA 5 : Programming : Task 1  Testing Math.random()</TITLE>

by: RiK ooo 
last post by:
Hi, i'm currently working on C# project and I want to make use of the Math.Round() function to display only the first to digits of a float value. Unfortunately this doesn't seem to be working for me. I haven't much experience in using the Timer class or the Math class. I may be overlooking something stupid and the solution is probably really...

by: Hystou 
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...

by: jinu1996 
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
 
by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...

by: isladogs 
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...

by: conductexam 
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...

by: adsilva 
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.
 
by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
 