Rounding up to the nearest 0.05 in JavaScript

前端 未结 7 1964
你的背包
你的背包 2020-12-17 10:19

Question
Does anyone know of a way to round a float to the nearest 0.05 in JavaScript?

Example

BEFORE | AFTER         


        
相关标签:
7条回答
  • 2020-12-17 10:35

    I would go for the standard of actually dividing by the number you're factoring it to, and rounding that and multiplying it back again after. That seems to be a proper working method which you can use with any number and maintain the mental image of what you are trying to achieve.

    var val = 26.14,
        factor = 0.05;
    
    val = Math.round(val / factor) * factor;
    

    This will work for tens, hundreds or any number. If you are specifically rounding to the higher number then use Math.ceil instead of Math.round.

    Another method specifically for rounding just to 1 or more decimal places (rather than half a place) is the following:

    Number(Number(1.5454545).toFixed(1));
    

    It creates a fixed number string and then turns it into a real Number.

    0 讨论(0)
  • 2020-12-17 10:37

    Multiply by 20, then divide by 20:

    (Math.ceil(number*20)/20).toFixed(2)
    
    0 讨论(0)
  • 2020-12-17 10:48

    Rob's answer with my addition:

    (Math.ceil(number*20 - 0.5)/20).toFixed(2)
    

    Otherwise it always rounds up to the nearest 0.05.

    ** UPDATE **

    Sorry has been pointed out this is not what the orig poster wanted.

    0 讨论(0)
  • 2020-12-17 10:53

    You need to put -1 to round half down and after that multiply by -1 like the example down bellow.

    <script type="text/javascript">
    
      function roundNumber(number, precision, isDown) {
        var factor = Math.pow(10, precision);
        var tempNumber = number * factor;
        var roundedTempNumber = 0;
        if (isDown) {
          tempNumber = -tempNumber;
          roundedTempNumber = Math.round(tempNumber) * -1;
        } else {
          roundedTempNumber = Math.round(tempNumber);
        }
        return roundedTempNumber / factor;
      }
    </script>
    
    <div class="col-sm-12">
      <p>Round number 1.25 down: <script>document.write(roundNumber(1.25, 1, true));</script>
      </p>
      <p>Round number 1.25 up: <script>document.write(roundNumber(1.25, 1, false));</script></p>
    </div>

    0 讨论(0)
  • 2020-12-17 10:55

    My solution and test:

    let round = function(number, precision = 2, rounding = 0.05) {
      let multiply = 1 / rounding;
    
      return parseFloat((Math.round(number * multiply) / multiply)).toFixed(precision);
    };
    

    https://jsfiddle.net/maciejSzewczyk/7r1tvhdk/40/

    0 讨论(0)
  • 2020-12-17 10:57

    I would write a function that does it for you by

    • move the decimal over two places (multiply by 100)
    • then mod (%) that inflatedNumber by 5 and get the remainder
    • subtract the remainder from 5 so that you know what the 'gap'(ceilGap) is between your number and the next closest .05
    • finally, divide your inflatedNumber by 100 so that it goes back to your original float, and voila, your num will be rounded up to the nearest .05.

      function calcNearestPointZeroFive(num){
          var inflatedNumber = num*100,
              remainder     = inflatedNumber % 5;
              ceilGap       = 5 - remainder
      
          return (inflatedNumber + ceilGap)/100
      }
      

    If you want to leave numbers like 5.50 untouched you can always add this checker:

    if (remainder===0){
        return num
    } else {
        var ceilGap  = 5 - remainder
        return (inflatedNumber + ceilGap)/100
    }
    
    0 讨论(0)
提交回复
热议问题