System.Math.Round bug

  • Thread starter Thread starter Radek Cerny
  • Start date Start date
R

Radek Cerny

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.

TIA

Radek
 
Radek Cerny said:
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.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

If you compile the following code and then look at it in Reflector or
ILDASM, I think you'll see it calling the right one:

using System;

class Test
{
static void Main()
{
double d = 0.2;
Console.WriteLine (Math.Round(d, 2));
}
}
 
Hi Radek,

yes you're missing something (not very) obvious, the intellisense doesn't
always show you the right overload. But i'm sure it is using the right. You
could check it with ildasm.
Is there, beside the intellisense another reason you think it's chosing the
wrong overload?

PS. You should better read the link, Jon gave you. This certanly is a case
where the Gui is superfluous. You could show the problem in simple Console
app with only one method.

Radek Cerny said:
Thanks Jon,

Demo Attached.
Definitely a bug; forgot to mention using .Net 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top