How to convert number to fixed decimal points in Javascript

Description

The toFixed() method returns a string representation of a number with a specified number of decimal points.

Example


var num = 10; 
console.log(num.toFixed(2)); //"10.00" 
/* w  ww  .ja v  a2  s .  co  m*/
num = 10.005; 
console.log(num.toFixed(2)); //"10.01" 
        

The code above generates the following result.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window