JavaScript has some inaccurate rounding behavior so I've been looking for a reliable solution without success. There are many answers in this SO post SO post but none cover all the edge cases as far as I can tell. I wrote the following which handles all the edge cases presented. Will it be reliable with edge cases I haven't tested?
JavaScript has some inaccurate rounding behavior so I've been looking for a reliable solution without success. There are many answers in this SO post but none cover all the edge cases as far as I can tell. I wrote the following which handles all the edge cases presented. Will it be reliable with edge cases I haven't tested?
JavaScript has some inaccurate rounding behavior so I've been looking for a reliable solution without success. There are many answers in this SO post but none cover all the edge cases as far as I can tell. I wrote the following which handles all the edge cases presented. Will it be reliable with edge cases I haven't tested?
Rounding javascriptJavaScript decimals
JavascriptJavaScript has some inaccurate rounding behavior so I've been looking for a reliable solution without success. There are many answers in this soSO post but none cover all the edge cases as far as I can tell. I wrote the following which handles all the edge cases presented. Will it be reliable with edge cases I haven't tested?
If this is a viable solution, any enhancements to make it more efficient would be appreciated. It's not fast (Timetime to run function 1000000 times: 778ms) but doesn't seem to be terrible either. If there is a better solution, please post.
Rounding javascript decimals
Javascript has some inaccurate rounding behavior so I've been looking for a reliable solution without success. There are many answers in this so post but none cover all the edge cases as far as I can tell. I wrote the following which handles all the edge cases presented. Will it be reliable with edge cases I haven't tested?
If this is a viable solution, any enhancements to make it more efficient would be appreciated. It's not fast (Time to run function 1000000 times: 778ms) but doesn't seem to be terrible either. If there is a better solution, please post.
Rounding JavaScript decimals
JavaScript has some inaccurate rounding behavior so I've been looking for a reliable solution without success. There are many answers in this SO post but none cover all the edge cases as far as I can tell. I wrote the following which handles all the edge cases presented. Will it be reliable with edge cases I haven't tested?
If this is a viable solution, any enhancements to make it more efficient would be appreciated. It's not fast (time to run function 1000000 times: 778ms) but doesn't seem to be terrible either. If there is a better solution, please post.
UPDATE
After more testing I found one answer which is accurate, simpler, and usually faster. I modified it to handle variable precision. Note that it will round negatives toward zero, e.g. round(-1835.665, 2)) ==> -1835.66
.
UPDATE2
I've modified the below function to handle negative precision and negative numbers
function round(number, precision) {
precision = precision ? precision : 0;
var sign = 1;
if (number < 0) {
sign = -1;
number = Math.abs(number);
}
if (precision >= 0){
return (+(Math.round(number + "e+"+ precision) + "e-" + precision)) * sign;
} else {
return (+(Math.round(number + "e-"+ Math.abs(precision)) + "e+" + Math.abs(precision))) * sign ;
}
}
The edge cases that seemed to give the most problem were the first two:
UPDATE
After more testing I found one answer which is accurate, simpler, and usually faster. I modified it to handle variable precision. Note that it will round negatives toward zero, e.g. round(-1835.665, 2)) ==> -1835.66
.
UPDATE2
I've modified the below function to handle negative precision and negative numbers
function round(number, precision) {
precision = precision ? precision : 0;
var sign = 1;
if (number < 0) {
sign = -1;
number = Math.abs(number);
}
if (precision >= 0){
return (+(Math.round(number + "e+"+ precision) + "e-" + precision)) * sign;
} else {
return (+(Math.round(number + "e-"+ Math.abs(precision)) + "e+" + Math.abs(precision))) * sign ;
}
}
The edge cases that seemed to give the most problem were the first two:
The edge cases that seemed to give the most problem were the first two: