How can I round down a number in Javascript?

前端 未结 11 1391
感动是毒
感动是毒 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-29 23:58

    This was the best solution I found that works reliably.

    function round(value, decimals) {
        return Number(Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals);
     }
    

    Credit to: Jack L Moore's blog

提交回复
热议问题