Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Number.prototype.toFixed()

Esta tradução está incompleta. Ajude atraduzir este artigo.

O método toFixed() formata um número utilizando notação de ponto fixo.

Sintaxe

numObj.toFixed([dígitos])

Parámetros

dígitos
Opcional. O número de dígitos que aparecem depois do ponto decimal; este pode ser um valor entre 0 e 20, inclusive, e algumas implementacões podem suportar uma variação de números maiores. Se este argumento for omitido, será tratado como 0. 

Retorno

A string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If numObj is greater than 1e+21, this method simply calls Number.prototype.toString() and returns a string in exponential notation.

Throws

RangeError
Se os digitos forem muito pequenos ou muito grandes. Valores entre 0 e 20, inclusive, não irão causar o RangeError. É permitido às implementações suportar valores maiores e menores.
TypeError
Se este método for chamado en um objeto que não é Number.

Exemplos

Utilizando toFixed

var numObj = 12345.6789;

numObj.toFixed();       // Retorna '12346': note o arredondamento, não possui nenhuma parte fracionária
numObj.toFixed(1);      // Retorna '12345.7': note o arredondamento
numObj.toFixed(6);      // Retorna '12345.678900': note que adicionou zeros
(1.23e+20).toFixed(2);  // Retorna '123000000000000000000.00'
(1.23e-10).toFixed(2);  // Retorna '0.00'
2.34.toFixed(1);        // Retorna '2.3'
2.35.toFixed(1);        // Retorna '2.4'. Note que arredonda para cima neste caso.
-2.34.toFixed(1);       // Retorna -2.3 (devido à precedência do operador, literais de números negativos não retornam uma string...)
(-2.34).toFixed(1);     // Retorna '-2.3' (...a menos que se utilize parênteses)

Especificações

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Definição incial. Implementada no JavaScript 1.5.
ECMAScript 5.1 (ECMA-262)
The definition of 'Number.prototype.toFixed' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Number.prototype.toFixed' in that specification.
Standard  
ECMAScript 2016 Draft (7th Edition, ECMA-262)
The definition of 'Number.prototype.toFixed' in that specification.
Draft  

Compatibilidade dos navegadores

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

Veja também

Etiquetas do documento e colaboradores

 Colaboradores desta página: akfzambrana
 Última atualização por: akfzambrana,