js math trunc

Solutions on MaxInterview for js math trunc by the best coders in the world

showing results for - "js math trunc"
Elena
06 Aug 2020
1The Math.trunc() function 
2returns the integer part of a number by removing any fractional digits.
3
4console.log(Math.trunc(13.37));
5// expected output: 13
6console.log(Math.trunc(42.84));
7// expected output: 42
8console.log(Math.trunc(0.123));
9// expected output: 0
10console.log(Math.trunc(-0.123));
11// expected output: -0
Kevin
30 Feb 2016
1Number.prototype.toFixedDown = function(digits) {
2    var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"),
3        m = this.toString().match(re);
4    return m ? parseFloat(m[1]) : this.valueOf();
5};
6
7[   5.467.toFixedDown(2),
8    985.943.toFixedDown(2),
9    17.56.toFixedDown(2),
10    (0).toFixedDown(1),
11    1.11.toFixedDown(1) + 22];
12
13// [5.46, 985.94, 17.56, 0, 23.1]
14