1 // value is the value to round
2 // places if positive the number of decimal places to round to
3 // places if negative the number of digits to round to
4 function roundTo(value, places){
5 var power = Math.pow(10, places);
6 return Math.round(value * power) / power;
7 }
8 var myNum = 10000/3; // 3333.3333333333335
9 roundTo(myNum, 2); // 3333.33
10 roundTo(myNum, 0); // 3333
11 roundTo(myNum, -2); // 3300
12
1 function ceilTo(value, places){
2 var power = Math.pow(10, places);
3 return Math.ceil(value * power) / power;
4 }
5 function floorTo(value, places){
6 var power = Math.pow(10, places);
7 return Math.floor(value * power) / power;
8 }
9
1var a = Math.round(2.3); // a is now 2
2var b = Math.round(2.7); // b is now 3
3var c = Math.round(2.5); // c is now 3
4
5var a = Math.ceil(2.3); // a is now 3
6var b = Math.ceil(2.7); // b is now 3
7
8Input : Math.floor(5.78)
9Output : 5
10
11Input : Math.floor(1.5)
12Output : 1