js arrotondare superiore numero 1

js arrotondare superiore numero

 // 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

Here is what the above code is Doing:
1. The function roundTo() takes two arguments: value and places.
2. The variable power is set to 10 to the power of places.
3. The value is multiplied by power and rounded to the nearest integer.
4. The value is then divided by power.
5. The function returns the rounded value.
6. The variable myNum is set to the result of 10000 divided by 3.
7. The function is called with myNum and 2 as arguments.
8. The function is called with myNum and 0 as arguments.
9. The function is called with myNum and -2 as arguments.

Similar Posts