JavaScript provides Number.toFixed(n), Number.toPrecision(n) and Math.round(n) functions for number formatting.

**Number.toFixed(n)** – n = Number of decimal digits required. Formats number for ‘n’ number of decimal digits. Number is rounded up and ’0′s are added after decimal point if needed to create the desired decimal length.

var n = 685.5428; var result; result=n.toFixed(2); //returns 685.54 result=n.toFixed(5); //returns 685.54280 (padding) result=n.toFixed(3); //returns 685.543 (round up) n = 547; result=n.toFixed(2); //returns 547.00

**Number.toPrecision(n)** – n = Number of significant digits required. Formats number so that it is of length ‘n’. A decimal point and ’0′s are used if needed to create the desired length.

var n = 398.456; var result; result=n.toPrecision(5); //returns 398.46 (round up) result=n.toPrecision(7); //returns 398.4560 (padding)

**Math.round(n)** – n = number to be rounded. Number is rounded off to the nearest integer using the “.5″ up rule.

var result; result=Math.round(14.67); //returns 15 (round up) result=Math.round(14.47); //returns 14

**Round number to ‘n’ decimal digits using Math.round**

To round any number to n decimal points, perform following steps.

1 – Multiple the original number by 10^n i.e 10 to the power of n

2 – Apply Math.round() to the result

3 – Divide result by 10^n

var result; result=Math.round(14.6789 * 100) /100; //returns 14.68 (round to two decimals) result=Math.round(14.6789 * 10) /10; //returns 14.7 (round to 1 decimal)