Number toFixed

Description

toFixed() format a number with a specified number of decimal points.

Example


var num = 10;
console.log(num.toFixed(2));    //"10.00"
num = 10.005;/*ww w  .  j  av a 2  s  .c  o  m*/
console.log(num.toFixed(2));    //"10.01"

toFixed(2) formats number with two decimal places, filling out the empty decimal places with zeros. It also rounds the value.

The toFixed() method can represent numbers with 0 through 20 decimal places.

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions