JavaScript Math.round()
Esempio 1
let x = Math.round(2.5);
Definizione e utilizzo
Il metodo Math.round() arrotonda un numero all'intero più vicino.
Nota: 2,49 verrà arrotondato per difetto (2) e 2,5 verrà arrotondato per eccesso (3).
Esempio 2
Arrotonda numeri diversi all'intero più vicino:
let a = Math.round(2.60);
let b = Math.round(2.50);
let c = Math.round(2.49);
let d = Math.round(-2.60);
let e = Math.round(-2.50);
let f = Math.round(-2.49);
Supporto browser
Math.round()
è una funzione ECMAScript1 (ES1).
ES1 (JavaScript 1997) è completamente supportato in tutti i browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
Sintassi
Math.round(x)
Parametri
Parameter | Description |
---|---|
x | Required. The number to be rounded |
Valore di ritorno
Un numero che rappresenta l'intero più vicino. |