js arrotondare numeri code example
Example 1: js arrotondare superiore numero
function roundTo(value, places){
var power = Math.pow(10, places);
return Math.round(value * power) / power;
}
var myNum = 10000/3;
roundTo(myNum, 2);
roundTo(myNum, 0);
roundTo(myNum, -2);
Example 2: js arrotondare superiore numero
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;
}
Example 3: js arrotondare numeri
var a = Math.round(2.3);
var b = Math.round(2.7);
var c = Math.round(2.5);
var a = Math.ceil(2.3);
var b = Math.ceil(2.7);
Input : Math.floor(5.78)
Output : 5
Input : Math.floor(1.5)
Output : 1