|
| 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.
|
|
|
| |
|
| |
| |
| [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]
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
| |
| [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]
|
|
|
| |
|
|
| |
|
|
|
| |
| 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 } }
|
|
|
| |
|
| |
| |
| 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.
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|