“JS Arrotondare Numeri” Réponses codées

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
DevLorenz02

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;
 }
DevLorenz02

JS Arrotondare Numeri

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

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

Input  : Math.floor(5.78)
Output : 5

Input  : Math.floor(1.5)
Output : 1
DevLorenz02

Réponses similaires à “JS Arrotondare Numeri”

Questions similaires à “JS Arrotondare Numeri”

Plus de réponses similaires à “JS Arrotondare Numeri” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code