views:

233

answers:

2

Hay, i need a hand.

I want to convert all numbers to the nearest .25

So...

5 becomes 5.00
2.25 becomes 2.25
4 becomes 4.00
3.5 becomes 3.50

Thanks

+14  A: 

Multiply by 4, round to integer, divide by 4 and format with two decimals.

Edit Any reason for the downvotes? At least leave a comment to know what should be improved.

rslite
Why has this been downvoted? It's the right answer, after all.
Duroth
Thanks! Was wondering the same thing...
rslite
+1: It's right for me too.
Steve
Maybe you were downvoted because you didn't do all his work for him?
random
@ e.c.ho - Yep. exact reason. Although the theory was correct. I've upvoted him now because he was technically correct.
dotty
@ rslite. Feel free to edit this post so i can upvote you.
dotty
+8  A: 

Here’s an implementation of what rslite said:

var number = 5.12345;
number = (Math.round(number * 4) / 4).toFixed(2);
Gumbo
Also work in Java - thanks.
Fedearne
@Fedearne: This is rather an arithmetic problem than a language problem.
Gumbo