Format a number to two decimal places

Use toFixed with 2 as the number of decimal places.


Alternatively you can use Intl.NumberFormat() with { style: 'percent'}

var num = 25;

var option = {
  style: 'percent'

};
var formatter = new Intl.NumberFormat("en-US", option);
var percentFormat = formatter.format(num / 100);
console.log(percentFormat);

input = 0.3;
output = input.toFixed(2);
//output: 0.30

You can use the toFixed() method on Number objects:

var array = [0.39, 2.5,  4.25, 5.5,  6.75, 7.75, 8.5], new_array = [];
for(var i = 0, j = array.length; i < j; i++) {
    if(typeof array[i] !== 'number') continue;
    new_array.push(array[i].toFixed(2));
}