Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Math.Round problem
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Eric Peng
Hi:
I found Round method in .NET is a little bit strange. For example:
Math.Round(4.45, 1); //return 4.4
Math.Round(4.55, 1); //return 4.6
This process is known as rounding toward even, or rounding to nearest.MSDN
said this method follows IEEE Standard 754, or banker's rounding. But, I
don't want the result is the rounded value that has an even digit in the far
right decimal position, the result I want is:
Math.Round(4.45, 1); //return 4.5
Math.Round(4.55, 1); //return 4.6
Is there existing method in .NET or other way could do like this?
Thanks in advance.

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
[MVP] Thomas Tomiczek (VIP)
Sorry to say, but when I was in school "bankers rounding" was that .5 is
rounded DOWN.

So 4.45 is rightfully 4.4

You might have to play around with other functions than rounding.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"Eric Peng" <Click here to reveal e-mail address> wrote in message
news:uwG6fMv#BHA.2192@tkmsftngp02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Eric Peng
Seems it isn't simple like you said: 5 is always rounded down. It is
rounding toward the nearest even number. 4.45 is rounded to 4.4, 4.35 is
rounded to 4.4 too. The result is decided by the digit in front of 5 is odd
or even. But generally don't care what the digit before 5 is. What I want is
are there some algorithms or existing methods in C# could do common
rounding, just like 4.45 is rounded to 4.5, 4.35 is rounded to 4.4.
Thanks anyway.

"[MVP] Thomas Tomiczek" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
[MVP] Thomas Tomiczek (VIP)
I hate to say it - you are right.

That IS a problem for all financial calculations - and I woulhd have hit
this today. Thanks.

Well, lets lave a look in the documentation. I seem to be unable to find a
"financial" rounding method. Hm, anyone else willing to pick this up?

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

"Eric Peng" <Click here to reveal e-mail address> wrote in message
news:uYHUOAx#BHA.1940@tkmsftngp04...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Christoph Nahr
On Tue, 14 May 2002 10:53:46 +0800, "Eric Peng"
<Click here to reveal e-mail address> wrote:

> I found Round method in .NET is a little bit strange.

Yes, it's very weird. Actually System.Math was the first place I've
ever seen this kind of "banker's rounding", and I'd definitely
complain if *my* banker did that with my bank account!

I guess there's no alternative but to write your own rounding routine.
Here's what I came up with -- not very fast but it works:

using System;

class MainClass {

public static double MyRound(double value) {
int sign = Math.Sign(value);
double round = Math.Floor(Math.Abs(value) + 0.5);
return (sign * round);
}

public static double MyRound(double value, int digits) {
int sign = Math.Sign(value);
double scale = Math.Pow(10.0, digits);
double round = Math.Floor(Math.Abs(value) * scale + 0.5);
return (sign * round / scale);
}

public static void Main() {
Console.WriteLine(MyRound(-4.45, 1).ToString()); // -4.5
Console.WriteLine(MyRound(-4.44, 1).ToString()); // -4.4
Console.WriteLine(MyRound(4.44, 1).ToString()); // 4.4
Console.WriteLine(MyRound(4.45, 1).ToString()); // 4.5
Console.WriteLine(MyRound(4.54, 1).ToString()); // 4.5
Console.WriteLine(MyRound(4.55, 1).ToString()); // 4.6
}
}
Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
Sathisha Krishnamurthy
(Type your message here)

--------------------------------
From: Sathisha Krishnamurthy

Hi Christoph Nahr

The same function does not seem to work with 8.665 where the answer is 8.66!! again when it is expected to be 8.67.
Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Console
System.Math




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734