1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
---
title: Number.prototype.toFixed()
slug: Web/JavaScript/Reference/Global_Objects/Number/toFixed
translation_of: Web/JavaScript/Reference/Global_Objects/Number/toFixed
original_slug: Web/JavaScript/Referencia/Objetos_globales/Number/toFixed
---
<div>{{JSRef}}</div>
<p>El método <strong><code>toFixed()</code></strong> formatea un número usando notación de punto fijo.</p>
<div>{{EmbedInteractiveExample("pages/js/number-tofixed.html")}}</div>
<h2 id="Sintaxis">Sintaxis</h2>
<pre class="syntaxbox"><code><var>numObj</var>.toFixed([<var>digitos</var>])</code></pre>
<h3 id="Parametros">Parametros</h3>
<dl>
<dt><code>digitos</code></dt>
<dd>Opcional. El número de digitos que aparecen después del punto decimal; este puede ser un valor entre 0 y 20, inclusive, algunas implementaciones pueden soportar un rango más amplio de valores. Si el argumento es omitido, es tratado como 0.</dd>
</dl>
<h3 id="Valor_Devuelto">Valor Devuelto</h3>
<p>Una cadena que representa el número dado, usando notación de punto fijo.</p>
<h3 id="Excepciones">Excepciones</h3>
<dl>
<dt>{{jsxref("RangeError")}}</dt>
<dd>Si <code>digits</code> es demasiado pequeño o demasiado grande. Los valores entre 0 y 20, inclusive, no causarán un error tipo<code> {{jsxref("RangeError")}}</code>. Las implementaciones también pueden admitir valores cada vez más grandes.</dd>
<dt>{{jsxref("TypeError")}}</dt>
<dd>Si este método se invoca en un objeto que no es un {{jsxref("Number")}}.</dd>
</dl>
<h2 id="Descripción">Descripción</h2>
<p><strong><code>toFixed()</code></strong> devuelve una representación de cadena de <code> numObj </code> que no usa notación exponencial y tiene exactamente <code> dígitos </code> dígitos después del decimal. El número se redondea si es necesario, y la parte fraccional se rellena con ceros si es necesario para que tenga la longitud especificada.Si <code>numObj</code> es mayor que <code>1e+21</code>, este metodo llama a {{jsxref("Number.prototype.toString()")}} y retorna una cadena de notacion exponencial.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Using_toFixed">Using <code>toFixed</code></h3>
<pre class="brush: js">var numObj = 12345.6789;
numObj.toFixed(); // Returns '12346': note rounding, no fractional part
numObj.toFixed(1); // Returns '12345.7': note rounding
numObj.toFixed(6); // Returns '12345.678900': note added zeros
(1.23e+20).toFixed(2); // Returns '123000000000000000000.00'
(1.23e-10).toFixed(2); // Returns '0.00'
2.34.toFixed(1); // Returns '2.3'
2.35.toFixed(1); // Returns '2.4'. Note that it rounds up in this case.
-2.34.toFixed(1); // Returns -2.3 (due to operator precedence, negative number literals don't return a string...)
(-2.34).toFixed(1); // Returns '-2.3' (...unless you use parentheses)
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initial definition. Implemented in JavaScript 1.5.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.7.4.5', 'Number.prototype.toFixed')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
{{Compat("javascript.builtins.Number.toFixed")}}
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Number.prototype.toExponential()")}}</li>
<li>{{jsxref("Number.prototype.toPrecision()")}}</li>
<li>{{jsxref("Number.prototype.toString()")}}</li>
</ul>
|