diff options
Diffstat (limited to 'files/es/web/javascript/reference/global_objects/math/floor')
-rw-r--r-- | files/es/web/javascript/reference/global_objects/math/floor/index.html | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/files/es/web/javascript/reference/global_objects/math/floor/index.html b/files/es/web/javascript/reference/global_objects/math/floor/index.html new file mode 100644 index 0000000000..03ad07325e --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/math/floor/index.html @@ -0,0 +1,125 @@ +--- +title: Math.floor() +slug: Web/JavaScript/Reference/Global_Objects/Math/floor +tags: + - JavaScript + - Math + - Method +translation_of: Web/JavaScript/Reference/Global_Objects/Math/floor +original_slug: Web/JavaScript/Referencia/Objetos_globales/Math/floor +--- +<div>{{JSRef("Objetos_globales", "Math")}}</div> + +<h2 id="Summary" name="Summary">Sumario</h2> + +<p>Devuelve el máximo entero menor o igual a un número.</p> + +<h2 id="Syntax" name="Syntax">Sintaxis</h2> + +<pre class="syntaxbox notranslate"><code>Math.floor(<em>x</em>) </code></pre> + +<h3 id="Parameters" name="Parameters">Parámetros</h3> + +<dl> + <dt><code>x</code></dt> + <dd>Es número.</dd> +</dl> + +<h2 id="Description" name="Description">Descripción</h2> + +<p>Como <code>floor</code> es un método estático de <code>Math</code>, siempre debe usarse como <code>Math.floor()</code>, en lugar de usarlo como un método de un objeto <code>Math</code> creado.</p> + +<h2 id="Examples" name="Examples">Ejemplos</h2> + +<h3 id="Example_Using_Math.floor" name="Example:_Using_Math.floor">Ejemplo: Usando <code>Math.floor</code></h3> + +<p>La siguiente función devuelve el valor entero redondeado más bajo de la variable <code>x</code>:</p> + +<pre class="brush:js notranslate">function getFloor(x) { + return Math.floor(x); +}</pre> + +<p>Si se pasa <code>45.95</code> a <code>getFloor</code>, éste devuelve <code>45</code>; si se le pasa <code>-45.95</code>, devuelve <code>-46</code>.</p> + +<h3 id="Example_Decimal_adjustment" name="Example:_Decimal_adjustment">Ejemplo: Ajuste decimal</h3> + +<pre class="brush:js notranslate">// Cierre +(function(){ + + /** + * Ajuste decimal de un número. + * + * @param {String} type El tipo de ajuste. + * @param {Number} value El número. + * @param {Integer} exp El exponente(el logaritmo en base 10 del ajuste). + * @returns {Number} El valor ajustado. + */ + function decimalAdjust(type, value, exp) { + // Si el exp es indefinido o cero... + if (typeof exp === 'undefined' || +exp === 0) { + return Math[type](value); + } + value = +value; + exp = +exp; + // Si el valor no es un número o el exp no es un entero... + if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { + return NaN; + } + // Cambio + value = value.toString().split('e'); + value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); + // Volver a cambiar + value = value.toString().split('e'); + return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); + } + + // Redondeo decimal + if (!Math.round10) { + Math.round10 = function(value, exp) { + return decimalAdjust('round', value, exp); + }; + } + // Redondeo hacia abajo + if (!Math.floor10) { + Math.floor10 = function(value, exp) { + return decimalAdjust('floor', value, exp); + }; + } + // Redondeo hacia arriba + if (!Math.ceil10) { + Math.ceil10 = function(value, exp) { + return decimalAdjust('ceil', value, exp); + }; + } + +})(); + +// Redondeo +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 +// Piso +Math.floor10(55.59, -1); // 55.5 +Math.floor10(59, 1); // 50 +Math.floor10(-55.51, -1); // -55.6 +Math.floor10(-51, 1); // -60 +// Techo +Math.ceil10(55.51, -1); // 55.6 +Math.ceil10(51, 1); // 60 +Math.ceil10(-55.59, -1); // -55.5 +Math.ceil10(-59, 1); // -50 +</pre> + +<h2 id="See_also" name="See_also">Ven También</h2> + +<ul> + <li>El {{jsxref("Math")}} objeto.</li> + <li>{{jsxref("Math.abs")}}</li> + <li>{{jsxref("Math.ceil()")}}</li> + <li>{{jsxref("Math.round()")}}</li> +</ul> |