How to format the number with two decimal points using JavaScript function

By | November 2, 2020

JavaScript has an in-built method called ‘toFixed()’ that help us formatting a number with two decimal points. toFixed() method allows us to format any number of decimal points.

Here is example code:

var number = 100.4355;
number = number.toFixed(2);
alert(number);

The output will be: 100.44

In the above example, I have passed the parameter as ‘2’ in ‘toFixed’ method, because I wanted to format the number with two decimal points. If your requirement is for one decimal point then pass ‘1’ as a parameter.

Also read: How to get a random item from an array using JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *