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]
# 1
http://forum.java.sun.com/thread.jspa?threadID=692276
pbrockway2a at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 2
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
Drake_Duna at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 3

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;

}

Laszlo.a at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 4
I prefer my Swedes pointy.(Sorry. Couldn't resist. In a weird mood. Must be the water.)
jverda at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 5
> I prefer my Swedes pointy.Overshare!!
Laszlo.a at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 6

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

kajbja at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 7
> 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
pbrockway2a at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 8

> > 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)

kajbja at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 9
(And I actually thought that was the way you rounded in e.g. US as well)
kajbja at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 10

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.

pbrockway2a at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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.

CeciNEstPasUnProgrammeura at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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

kajbja at 2007-7-13 3:28:54 > top of Java-index,Java Essentials,Java Programming...