Rounding to the nearest 0.05 in JavaScript

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

Example

BEFORE | AFTER 2.51 | 2.55 2.50 | 2.50 2.56 | 2.60 

Current code

 var _ceil = Math.ceil; Math.ceil = function(number, decimals){ if (arguments.length == 1) return _ceil(number); multiplier = Math.pow(10, decimals); return _ceil(number * multiplier) / multiplier; } 

Then in another place ... return (Math.ceil((amount - 0.05), 1) + 0.05).toFixed(2);

That leads to...

 BEFORE | AFTER 2.51 | 2.55 2.50 | 2.55 2.56 | 2.65 
+12
source share
7 answers

Multiply by 20, then divide by 20:

 (Math.ceil(number*20)/20).toFixed(2) 
+31
source

Rob will answer with my add-on:

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

Otherwise, it is always rounded to the nearest 0.05.

** UPDATE **

Sorry, it was noted that this is not what the original author wanted.

+8
source

I would choose the standard of actual division by the number by which you divide it, and rounding, and then multiplying. This seems to be the right method of work that you can use with any number and maintain a 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 specifically round the number above, use Math.ceil instead of Math.round .

Another method specifically designed to round to 1 or more decimal places (rather than half) is as follows:

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

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

+8
source

I would write a function that does this for you

  • move the decimal to two places (multiply by 100)
  • then mod ( % ) that inflatedNumber is 5 and we get the remainder
  • subtract remainder from 5 so that you know that a “space” ( ceilGap ) is between your number and the nearest nearest .05
  • finally, divide your inflatedNumber by 100 so that it returns to the original float, and voila, your number will be rounded 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, for example, 5.50 intact, you can always add this checker:

 if (remainder===0){ return num } else { var ceilGap = 5 - remainder return (inflatedNumber + ceilGap)/100 } 
+2
source

You need to put -1 in the round half and then multiply by -1, as an example below.

 <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> 
+1
source

I have successfully used this function in my project:

 roundToNearestFiveCents( number: any ) { return parseFloat((Math.round(number / 0.05) * 0.05).toFixed(2)); } 

It may be useful for those who just want to round up to 5 cents for their monetary results, save the result as a number, so if you add it additionally, this will not lead to string concatenation; also not unnecessarily rounding as indicated in several other answers. It is also limited to two decimal places, which is usually for finance.

0
source

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
source

Source: https://habr.com/ru/post/914716/


All Articles