Display numbers up to two decimals places without trailing zeros

Multiply by 100, floor, divide by 100.

var n = 8.7456;
var result = Math.floor(n * 100) / 100; // 8.74

Edit: if you’re looking at this question after the fact, this is probably not what you want. It satisfies the odd requirement of having 8.7456 appear as 8.74. See also the relevant comment.


Use Number.toFixed to round the number up to two digits and format as a string. Then use String.replace to chop off trailing zeros:

[8.7456, 8.745, 8.74, 8.7, 8].forEach(function(num) {
  var str = num.toFixed(2).replace(/\.?0+$/, "");
  console.log(num, str);
});