La funzione Math.round()
restituisce il valore di un numero approssimato all'intero ad esso più vicino.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Sintassi
Math.round(x)
Parametri
x
- Un numero.
Valore restituito
Il valore del numero dato approssimato all'intero più vicino.
Descrizione
Se la parte frazionale del numero è maggiore di 0.5, l'argomento (x) è approssimato all'intero successivo con il valore assoluto più elevato. Se è inferiore di 0.5, l'argomento è approssimato all'intero con il valore assoluto più basso. Se la parte frazionale è esattamente 0.5, l'argomento è approssimato all'intero successivo nella direzione di +∞. Si noti che questo è diverso da quanto accade nelle funzioni round()
di molti altri linguaggi, che spesso invece approssimano questo caso all'intero successivo più lontano da zero, (dando un risultato diverso nel caso dei numeri negativi con una parte frazionale di esattamente 0.5).
Poiché round()
è un metodo statico di Math
, lo si usa sempre come Math.round()
, piuttosto che come un metodo di un Oggetto Math
appositamente creato (Math
non ha un costruttore).
Esempi
Math.round( 20.49); // 20
Math.round( 20.5 ); // 21
Math.round( 42 ); // 42
Math.round(-20.5 ); // -20
Math.round(-20.51); // -21
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) The definition of 'Math.round' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.round' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'Math.round' in that specification. |
Living Standard |
Browser compatibility
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.