js math.trunc code example
Example 1: javascript number if .00 truncate
Number.prototype.toFixedDown = function(digits) {
var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"),
m = this.toString().match(re);
return m ? parseFloat(m[1]) : this.valueOf();
};
[ 5.467.toFixedDown(2),
985.943.toFixedDown(2),
17.56.toFixedDown(2),
(0).toFixedDown(1),
1.11.toFixedDown(1) + 22];
Example 2: js math.trunc
The Math.trunc() function
returns the integer part of a number by removing any fractional digits.
console.log(Math.trunc(13.37));
console.log(Math.trunc(42.84));
console.log(Math.trunc(0.123));
console.log(Math.trunc(-0.123));