How can I round down a number in Javascript?

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

    Round towards negative infinity - Math.floor()

    +3.5 => +3.0
    -3.5 => -4.0
    

    Round towards zero - usually called Truncate(), but not supported by JavaScript - can be emulated by using Math.ceil() for negative numbers and Math.floor() for positive numbers.

    +3.5 => +3.0 using Math.floor()
    -3.5 => -3.0 using Math.ceil()
    

提交回复
热议问题