Swedish rounding
Hi all,
I want to round some numbers to the nearest five. Numbers have two decimal points.
Rounding should be like this
1 or 2 cents ?rounded down to 0.
3 or 4 cents ?rounded up to 5.
6 or 7 cents ?rounded down to 5.
8 or 9 cents ?rounded up to 10.
e.g: 20.56 = 20.50
20.55 = 20.55
20.58 = 20.60
I can do this using Strings. But i like to know is there anyway in java to do this in some other easy way.
Thanks in advance
[493 byte] By [
Garukaa] at [2007-10-2 10:59:57]

http://forum.java.sun.com/thread.jspa?threadID=692276
There should be some way to do this using multiplication, division, modulus, and an if statement, but I am too lazy to do it myself.Drake
public static double myRound(double num)
{
int iNum = (int)(num*(double)100);
int mod = iNum % 5;
if(mod < 3)
iNum -= mod;
else
iNum += (5-mod);
return (double)iNum/(double)100;
}
I prefer my Swedes pointy.(Sorry. Couldn't resist. In a weird mood. Must be the water.)
> I prefer my Swedes pointy.Overshare!!
Swedish rounding? Never heard of.
> Hi all,
> I want to round some numbers to the nearest five.
> Numbers have two decimal points.
> Rounding should be like this
> 1 or 2 cents ?rounded down to 0.
> 3 or 4 cents ?rounded up to 5.
> 6 or 7 cents ?rounded down to 5.
> 8 or 9 cents ?rounded up to 10.
>
> e.g: 20.56 = 20.50
How come 20.56 isn't rounded to 20.55?
> 20.55 = 20.55
> 20.58 = 20.60
> I can do this using Strings. But i like to know is
> there anyway in java to do this in some other easy
> way.
> Thanks in advance
Kaj
> Swedish rounding? Never heard of. Hi from New Zealand - home, apparently, of Swedish rounding. http://michaelandrews.blogspot.com/2005/07/swedish-rounding-world-famous-in-new.html
> > Swedish rounding? Never heard of.
>
> Hi from New Zealand - home, apparently, of Swedish
> rounding.
> http://michaelandrews.blogspot.com/2005/07/swedish-rou
> nding-world-famous-in-new.html
Thanks for the link.
It's true that we would use that rounding if we had cents, but the smallest value of coins in Sweden is 50 鰎e. (100 鰎e = 1 kr) , and they removed the 5, and 10 鰎e coins a long time ago. (1 kr is about 1/9 USD)
(And I actually thought that was the way you rounded in e.g. US as well)
I didn't realise you were Swedish - also found this (you may make more
of it than me!)
http://www.sourze.se/default.asp?ItemID=10204627
So, do you round to the nearest 50 鰎e? Or do you have the good sense
not to price things with a precision greater than your currency? NZ
has had this rounding system in place for about 15 years, but things
sell for eg, $12.34.
My Googling confirms what was said in the earlier blog - the references
(at least to the term) refer to NZ.
> So, do you round to the nearest 50 鰎e? Or do you
> have the good sense
> not to price things with a precision greater than
> your currency? NZ
> has had this rounding system in place for about 15
> years, but things
> sell for eg, $12.34.
The swiss do it that way, too. It makes sense if you consider that you might be buying more than one thing.
> I didn't realise you were Swedish -
Np
>also found this (you may make more
>of it than me!)
>http://www.sourze.se/default.asp?ItemID=10204627
That was actually an article about being proud of your origins :)
> So, do you round to the nearest 50 鰎e?
When we pay yes, but not on the prices in the grocery store. You can still see prices like carrots 5.38 kr / kg.
Kaj