Formatting Decimals
JS · Numbers & Mathsyntax
num.toFixed(digits)
num.toPrecision(precision)example
const total = 29.5;
console.log(total.toFixed(2)); // "29.50"
console.log((0.1 + 0.2).toFixed(2)); // "0.30"
const big = 123456.789;
console.log(big.toPrecision(6)); // "123457"Note toFixed() returns a STRING, not a number. Wrap in Number() or use + to convert back: +total.toFixed(2)