qc-round
Rounds a number to a specified number of decimal places.
Installation
npm install --save qc-round
Comparison with JavaScript's Math.round
Differences
-
Math.round
doesn't allow rounding to be done at different decimal positions. It has to be simulated.-
Math.round(value * 100) / 100
to simulateround(value, -2)
; -
Math.round(value / 100) * 100
to simulateround(value, 2)
;
-
-
Due to the underlying floating point number representation, using the simulated examples above with certain values does not return the correct value.
- E.g.,
Math.round(1.005 * 100) / 100
returns1
instead of1.01
.
- E.g.,
-
Will not ever return
-0
.-
E.g.,
Math.round(-0)
returns-0
instead of0
. -
E.g.,
Math.round(-Number.MIN_VALUE)
returns-0
instead of0
.
-
Example Usage
; ; // 20; // 21; // -20; // -21; // 1234.5678; // 1234.5678; // 1234.568; // 1234.57; // 1234.6; // 1235; // 1235; // 1230; // 1200; // 1000; // 0