Always display at least two decimal places

decimal, javascript

Solution

You could fix to 2 or the count of current places;

 var result = num.toFixed(Math.max(2, (num.toString().split('.')[1] || []).length));

Problem

I want to format a number so that it always have at least two decimal places. Samples: ``` 1 2.1 123.456 234.45 ``` Output: ``` 1.00 2.10 123.456 234.45 ```

Original source

Related problems