Truncate/round whole number in JavaScript?

前端 未结 6 1039
长发绾君心
长发绾君心 2020-12-24 00:11

For a script I\'m writing, I need display a number that has been rounded, but not the decimal or anything past it. I\'ve gotten down to rounding it to the third place, but I

6条回答
  •  猫巷女王i
    2020-12-24 00:46

    Math.trunc() function removes all the fractional digits.

    For positive number it behaves exactly the same as Math.floor():

    console.log(Math.trunc(89.13349)); // output is 89
    

    For negative numbers it behaves same as Math.ceil():

    console.log(Math.trunc(-89.13349)); //output is -89
    

提交回复
热议问题