jquery - javascript: rounding a float UP to nearest .25 (or whatever...) -


all answers can find rounding nearest, not value... example

1.0005 => 1.25  (not 1.00) 1.9   => 2.00 2.10 => 2.25 2.59 => 2.75   etc. 

would seem pretty basic, has me stumped. has round up, not nearest, relatively easy thing do.

divide number 0.25 (or whatever fraction want round nearest to).

round nearest whole number.

multiply result 0.25.

math.ceil(1.0005 / 0.25) * 0.25 // 1.25  math.ceil(1.9 / 0.25) * 0.25 // 2  // etc. 

function tonearest(num, frac) {    return math.ceil(num / frac) * frac;  }    var o = document.getelementbyid("output");    o.innerhtml += "1.0005 => " + tonearest(1.0005, 0.25) + "<br>";  o.innerhtml += "1.9 => " + tonearest(1.9, 0.25) + "<br>";  o.innerhtml += "2.10 => " + tonearest(2.10, 0.25) + "<br>";  o.innerhtml += "2.59 => " + tonearest(2.59, 0.25);
<div id="output"></div>


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo