Format number to always show 2 decimal places

The Solution to Format number to always show 2 decimal places is


(Math.round(num * 100) / 100).toFixed(2);

Live Demo

_x000D_
_x000D_
var num1 = "1";_x000D_
document.getElementById('num1').innerHTML = (Math.round(num1 * 100) / 100).toFixed(2);_x000D_
_x000D_
var num2 = "1.341";_x000D_
document.getElementById('num2').innerHTML = (Math.round(num2 * 100) / 100).toFixed(2);_x000D_
_x000D_
var num3 = "1.345";_x000D_
document.getElementById('num3').innerHTML = (Math.round(num3 * 100) / 100).toFixed(2);
_x000D_
span {_x000D_
    border: 1px solid #000;_x000D_
    margin: 5px;_x000D_
    padding: 5px;_x000D_
}
_x000D_
<span id="num1"></span>_x000D_
<span id="num2"></span>_x000D_
<span id="num3"></span>
_x000D_
_x000D_
_x000D_

Note that it will round to 2 decimal places, so the input 1.346 will return 1.35.

~ Answered on 2011-05-26 05:27:34


Most Viewed Questions: