Re: rounding



That's just kind of odd....


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.ttev8lu58jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Mon, 04 Jun 2007 11:54:43 -0700, Nicholas Paldino [.NET/C# MVP]
<mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

You have to explain how the first and last cases are supposed to
work. Rounding .5625 to two places should result in .57 and rounding
6.125 to two places should result in 6.13, using mathematical rounding.
What rounding are you doing?

It seems to me that he wants the rounding to round to multiples of 0.05.

To the OP, you can accomplish this in the specific case like this:

double RoundToFiveHundredths(double num)
{
return Math.Round(num * 2.0, 1) / 2.0;
}

Assuming you are always dealing with multiples that are themselves
divisible into 1.0, you can do it generally like this:

double RoundToMultiple(double num, double multiple)
{
double scale = 1.0 / multiple;

return Math.Round(num * scale) / scale;
}

Note that if you pick something that is theoretically divisible into 1.0,
but which is not representable in binary (eg 1/3, or 0.333...), you may
wind up wind rounding error. But this would be unavoidable, since even if
rounded perfectly, you would not be able to represent the result exactly
in binary form (the same thing holds true for decimal types, though with
different specific values of course).

Pete


.



Relevant Pages

  • Re: sprintf rounding behavior
    ... first i guessed that this is an inherent problem due to the binary ... been no round-off errors at all, and this would place them exactly at the ... half way points when it comes to rounding. ... errors in originally calculating the odd multiples of .05. ...
    (comp.soft-sys.matlab)
  • Re: rounding
    ... Rounding .5625 to two places should result in .57 and rounding 6.125 to two places should result in 6.13, ... double RoundToMultiple(double num, double multiple) ... return Math.Round(num * scale) / scale; ... Note that if you pick something that is theoretically divisible into 1.0, but which is not representable in binary, you may wind up wind rounding error. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Rounding error when converting from double to int
    ... 8.875% of it (suppose it's a sales tax calculation), ... are always multiples of 0.0001%). ... so there is no rounding. ... The multiplication should be done in mathematics, ...
    (comp.lang.c)
  • Re: Rounding error when converting from double to int
    ... if you have $9.51 (represented as 951 cents) and you need to take ... 8.875% of it (suppose it's a sales tax calculation), ... are always multiples of 0.0001%). ... so there is no rounding. ...
    (comp.lang.c)
  • Re: ROUNDING IN EXCEL
    ... (multiples of 10) ... VBA Round function does not handle slight discrepancies in the binary ... does anyone have evidence that ASTM rounding has ...
    (microsoft.public.excel.misc)