Re: System.Math.Round bug (repost)
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Mon, 16 Oct 2006 07:38:04 +0100
Radek Cerny <radek.cerny@xxxxxxxxxxxxxxxxx> wrote:
Hopefully I'm just missing something obvious, but this method (or more
likely the CLR/complier) has a bug where I can not use the overloaded
version:
Round( double value, int digits )
It always seems to use the Round(decimal value, int decimals) version, no
matter what the first parameter is - double or decimal. Intellisense agrees
with the compiler - incorrectly.
Is it me, a known issue or a new issue? I need this quite desparately; I do
have a workaround where I check the scale of the number and do fancy stuff
assuming it will Round to decimal places rather than digits but its just too
ugly to bear wth for long.
Simple console app to reproduce attached. A double of value 12.34 is
rounded to 2 digits, but produces 12.34 incorrectly. Should produce
12.
Okay, firstly there's no need to attach a zip file just for the sake of
posting 420 bytes of code. For the sake of anyone else who doesn't want
to have to save an attachment, open the zip file and then view the
file, here's the code in question:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double d = 12.34;
int digits = 2;
double result = System.Math.Round(d, digits);
Console.Write
("System.Math.Round(double 12.34, int digits 2) should produce "+
"12.00, but produces " + result.ToString());
Console.ReadLine();
}
}
}
(Changed very slightly to avoid wrapping).
Note that if you run ildasm on the code you'll see that it *is* using
Math.Round(double, int). I don't know where you got the idea that the
compiler is doing the wrong thing - if it *did* use Math.Round(decimal,
int) you'd get the same result anyway.
Now, it looks like the problem is in the docs - it's rounding to a
specific number of decimal places, not significant figures.
Interestingly, the examples demonstrate this behaviour.
I suggest you file a documentation bug at
http://connect.microsoft.com/VisualStudio
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Prev by Date: Re: System.Math.Round bug (repost)
- Next by Date: Change the scrollbars' UI
- Previous by thread: Re: System.Math.Round bug (repost)
- Next by thread: Change the scrollbars' UI
- Index(es):
Relevant Pages
|
Loading