Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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?

deleted 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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.

deleted 819 characters in body
Source Link

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:

added 151 characters in body
Source Link
Loading
added 159 characters in body
Source Link
Loading
added 1 character in body
Source Link
Loading
deleted 47 characters in body
Source Link
Loading
deleted 47 characters in body
Source Link
Loading
added 85 characters in body
Source Link
Loading
added 85 characters in body
Source Link
Loading
added 520 characters in body
Source Link
Loading
Updated for negative numbers to round away from 0
Source Link
Loading
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /