[removed] Rounding Down in .5 Cases

前端 未结 4 2015
借酒劲吻你
借酒劲吻你 2020-12-19 05:08

I am in a situation where a JavaScript function produces numbers, such as 2.5. I want to have these point five numbers rounded down to 2, rather t

4条回答
  •  我在风中等你
    2020-12-19 05:17

    You can also use this function to round with no decimal part and .5 down rule (Only positive numbers):

    function customRound(number) {
       var decimalPart = number % 1;
       if (decimalPart === 0.5)
          return number - decimalPart;
       else
          return Math.round(number);
    }
    

    And sorry for my english.

提交回复
热议问题