Resum
La funció Math.floor()
retorna el nombre més gran dels nombres més petits o iguals a un nombre donat.
Sintaxi
Math.floor(x)
Paràmetres
x
- Un nombre.
Descripció
Degut a que floor()
és un mètode estàtic de Math
, sempre s'utilitza com a Math.floor()
, en comptes de com a mètode d'una instància de Math
(Math
no és un constructor).
Exemples
Exemple: Utilitzar Math.floor()
Math.floor( 45.95); // 45
Math.floor(-45.95); // -46
Exemple: Ajust decimal
// Closure
(function() {
/**
* Ajust decimal d'un nombre.
*
* @param {String} type El tipus d'ajust.
* @param {Number} value El nombre.
* @param {Integer} exp L'exponent (L'algoritme en base 10 de la base d'ajust
* @returns {Number} El valor ajustat.
*/
function decimalAdjust(type, value, exp) {
// Si exp és undefined o zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// Si value no és un nombre o exp no és un nombre sencer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Desplaçament
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Desfer el desplaçament
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Arrodoniment decimal
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Arrodoniment decimal a la baixa
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Arrodoniment decimal a l'alça
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
// Arrodoniments
Math.round10(55.55, -1); // 55.6
Math.round10(55.549, -1); // 55.5
Math.round10(55, 1); // 60
Math.round10(54.9, 1); // 50
Math.round10(-55.55, -1); // -55.5
Math.round10(-55.551, -1); // -55.6
Math.round10(-55, 1); // -50
Math.round10(-55.1, 1); // -60
// Arrodoniments a la baixa
Math.floor10(55.59, -1); // 55.5
Math.floor10(59, 1); // 50
Math.floor10(-55.51, -1); // -55.6
Math.floor10(-51, 1); // -60
// Arrodoniments a l'alça
Math.ceil10(55.51, -1); // 55.6
Math.ceil10(51, 1); // 60
Math.ceil10(-55.59, -1); // -55.5
Math.ceil10(-59, 1); // -50
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
ECMAScript 1a Edició | Standard | Definició inicial. Implementat a JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) The definition of 'Math.floor' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.floor' in that specification. |
Standard |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |