JavaScript Arithmetic (Math) Rounding

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Rounding

Math.round() will round the value to the closest integer using half round up to break ties.

var a = Math.round(2.3);       // a is now 2  
var b = Math.round(2.7);       // b is now 3
var c = Math.round(2.5);       // c is now 3

But

var c = Math.round(-2.7);       // c is now -3
var c = Math.round(-2.5);       // c is now -2

Note how -2.5 is rounded to -2. This is because half-way values are always rounded up, that is they're rounded to the integer with the next higher value.


Rounding up

Math.ceil() will round the value up.

var a = Math.ceil(2.3);        // a is now 3
var b = Math.ceil(2.7);        // b is now 3

ceiling a negative number will round towards zero

var c = Math.ceil(-1.1);       // c is now 1

Rounding down

Math.floor() will round the value down.

var a = Math.floor(2.3);        // a is now 2
var b = Math.floor(2.7);        // b is now 2

flooring a negative number will round it away from zero.

var c = Math.floor(-1.1);       // c is now -1

Truncating

Caveat: using bitwise operators (except >>>) only applies to numbers between -2147483649 and 2147483648.

2.3  | 0;                       // 2 (floor)
-2.3 | 0;                       // -2 (ceil)
NaN  | 0;                       // 0
6

Math.trunc()

Math.trunc(2.3);                // 2 (floor)
Math.trunc(-2.3);               // -2 (ceil)
Math.trunc(2147483648.1);       // 2147483648 (floor)
Math.trunc(-2147483649.1);      // -2147483649 (ceil)
Math.trunc(NaN);                // NaN

Rounding to decimal places

Math.floor, Math.ceil(), and Math.round() can be used to round to a number of decimal places

To round to 2 decimal places:

 var myNum = 2/3;               // 0.6666666666666666
 var multiplier = 100;
 var a = Math.round(myNum * multiplier) / multiplier;  // 0.67
 var b = Math.ceil (myNum * multiplier) / multiplier;  // 0.67
 var c = Math.floor(myNum * multiplier) / multiplier;  // 0.66

You can also round to a number of digits:

 var myNum = 10000/3;           // 3333.3333333333335
 var multiplier = 1/100;
 var a = Math.round(myNum * multiplier) / multiplier;  // 3300
 var b = Math.ceil (myNum * multiplier) / multiplier;  // 3400
 var c = Math.floor(myNum * multiplier) / multiplier;  // 3300

As a more usable function:

 // value is the value to round
 // places if positive the number of decimal places to round to
 // places if negative the number of digits to round to
 function roundTo(value, places){
     var power = Math.pow(10, places);
     return Math.round(value * power) / power;
 }
 var myNum = 10000/3;    // 3333.3333333333335
 roundTo(myNum, 2);  // 3333.33
 roundTo(myNum, 0);  // 3333
 roundTo(myNum, -2); // 3300

And the variants for ceil and floor:

 function ceilTo(value, places){
     var power = Math.pow(10, places);
     return Math.ceil(value * power) / power;
 }
 function floorTo(value, places){
     var power = Math.pow(10, places);
     return Math.floor(value * power) / power;
 }


Got any JavaScript Question?