Re: System.Math.Round bug (repost)



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
.



Relevant Pages

  • Re: check decimal and if numeric
    ... so effectively truncates an subsequent digits for positive numbers. ... It's different when you round negative numbers down, ... decimals, who's to say the third or fourth digit was the one that was in error? ...
    (microsoft.public.access.formscoding)
  • Re: Monetary calculations in CL
    ... you can implement the rounding rules ... E.g. suppose you are to round to the nearest 0.01 such that: ... (defun round-even-odd-rule (input digits) ... (multiple-value-bind (int frac) ...
    (comp.lang.lisp)
  • Re: Truncating Numbers
    ... digits to the right of the decimal. ... decimals), but the number is still 12.3546. ... extra 2 digits at the end can cause the "across" sums ... There are 2 methods to round the computation. ...
    (microsoft.public.access.queries)
  • Re: Preprocessor limitation workarounds
    ... #ifndef RADIX ... typedef unsigned short int digit_t; ... # error "Unable to represent the product of two digits" ...
    (comp.lang.c)
  • Re: binary number
    ... Integer literals ... An integer literal is a sequence of digits that has no period ... specifies its base and a suffix that specifies its type. ... these types in which its value can be represented: int, ...
    (comp.lang.cpp)

Loading