math.round

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

Hello all,

I am noticing different rounding from what the .net framework uses and what
client-side IE JScript uses. Is this by design? Try rounding 622.085. .Net
returns 622.08 and Jscript returns 622.09

Is there a way to get the 2 to be identical?

TIA,
Param
 
Mikhail Golovnykh said:
1.065, 1.075, 1.085 - rounding returns 1.06, 1.07, 1.08
1.025, 1.035, 1.045 - rounding returns 1.03, 1.04, 1.05
Got it?

In C# that's not what happens. (You didn't specify which
language/library you were talking about. Given the sample from the OP,
I don't think it's what would happen in JScript either.) In C# you'd
get 1.06, 1.08, 1.08, and 1.02, 1.04, 1.04 - at least if you use
Math.Round, which gives Banker's Rounding.

Test program (using decimals to avoid possible loss of accuracy):

using System;

public class Test
{
static void Main()
{
Round (1.065m);
Round (1.075m);
Round (1.085m);
Round (1.025m);
Round (1.035m);
Round (1.045m);
}

static void Round (decimal m)
{
Console.WriteLine ("{0} -> {1}", m, Math.Round (m, 2));
}
}

To get JScript to do the same, you'd need to implement your own
Banker's Rounding model, which wouldn't be too hard to do,
admittedly...
 
What is banker's rounding and where can I find an algorithm to implement it?
Is it a standard used by all banks?
 
RP said:
What is banker's rounding and

Basically it rounds to an even number when it has to decide between two
equally "good" options, so 8.5 would round to 8, 9.5 would round to 10,
and 10.5 would round to 10, etc.
where can I find an algorithm to implement it?

Well, you need to find some way of working out whether or not you've
got a half somewhere, and then work out which way to round. How you do
that will really depend on the language you're using, existing
libraries, and what format your numbers are in.
Is it a standard used by all banks?

That I don't know, to be honest.
 
Back
Top