How can I round down a number in Javascript?

前端 未结 11 1401
感动是毒
感动是毒 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:18

    Was fiddling round with someone elses code today and found the following which seems rounds down as well:

    var dec = 12.3453465,
    int = dec >> 0; // returns 12
    

    For more info on the Sign-propagating right shift(>>) see MDN Bitwise Operators

    It took me a while to work out what this was doing :D

    But as highlighted above, Math.floor() works and looks more readable in my opinion.

提交回复
热议问题