Javascript - Number toFixed() Method

The toFixed() method converts a number into a string keeping a specified number of decimals.

Description

The toFixed() method converts a number into a string keeping a specified number of decimals.

Syntax

number.toFixed(x)

Parameter Values

Parameter Require Description
x Optional. The number of digits after the decimal point. Default is 0, means no digits after the decimal point

Return

A String, representing a number, with the exact number of decimals

Demo

//Convert a number into a string, keeping only two decimals:
var num = 5.56789;
var n = num.toFixed(2);
console.log(n);/*  w w  w .  ja  v a2  s  . c  o  m*/

//Convert a number, without keeping any decimals:

var num = 5.56789;
var n = num.toFixed();
console.log(n);

//Convert a number which has fewer decimal places than requested:
var num = 5.56789;
var n = num.toFixed(10);
console.log(n);

Result