How can I round down a number in Javascript?

前端 未结 11 1400
感动是毒
感动是毒 2020-11-29 23:27

How can I round down a number in JavaScript?

math.round() doesn\'t work because it rounds it to the nearest decimal.

I\'m not sure if there is

11条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 00:10

    To round down towards negative infinity, use:

    rounded=Math.floor(number);
    

    To round down towards zero (if the number can round to a 32-bit integer between -2147483648 and 2147483647), use:

    rounded=number|0;
    

    To round down towards zero (for any number), use:

    if(number>0)rounded=Math.floor(number);else rounded=Math.ceil(number);
    

提交回复
热议问题