I would like to find a function that will return this kind of formatted values :
1.5555 => 1.55
1.5556 => 1.56
1.5554 => 1.55
1.5651 => 1.56
toFixed() and math round return this value :
1.5651.fixedTo(2) => 1.57
This will be usefull for money rounding.
How about this?
function wacky_round(number, places) {
var multiplier = Math.pow(10, places+2); // get two extra digits
var fixed = Math.floor(number*multiplier); // convert to integer
fixed += 44; // round down on anything less than x.xxx56
fixed = Math.floor(fixed/100); // chop off last 2 digits
return fixed/Math.pow(10, places);
}
1.5554 => 1.55
1.5555 => 1.55
1.5556 => 1.56
1.5651 => 1.56
So, that works, but I think you'll find that it's not a generally-accepted way to round. http://en.wikipedia.org/wiki/Rounding#Tie-breaking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With