Javascript Intelligent Rounding

I currently need to round the numbers to their nearest main number. (Not sure if there is a correct term here)

But look at an example of what I'm trying to achieve.

IE: 13 // 20 349 // 400 5645 // 6000 9892 // 10000 13988 // 20000 93456 // 100000 231516 // 300000 etc. etc. 

I implemented a way to do this, but it is so painful and only processes numbers up to a million, and if I want it to be higher, I need to add additional if statements (yes, let's see how I applied it: P im not very proud, but the brain is stuck)

There should be something there already, but google doesnโ€™t help me much, probably due to the fact that I donโ€™t know the correct term for such rounding that I want to do

+7
source share
5 answers
 <script type="text/javascript"> function intelliRound(num) { var len=(num+'').length; var fac=Math.pow(10,len-1); return Math.ceil(num/fac)*fac; } alert(intelliRound(13)); alert(intelliRound(349)); alert(intelliRound(5645)); // ... </script> 

See http://jsfiddle.net/fCLjp/

+14
source

One way:

 var a = [13, // 20 349, // 400 5645, // 6000 9892, // 10000 13988, // 20000 93456, // 100000 231516 // 300000 ] for (var i in a) { var num = a[i]; var scale = Math.pow(10, Math.floor(Math.log(num) / Math.LN10)); print([ num, Math.ceil(num / scale) * scale ]) } 13,20 349,400 5645,6000 9892,10000 13988,20000 93456,100000 231516,300000 
+8
source

you can use the Math.ceil function as described here:

javascript - dollar amount ceiling

to get the correct numbers, you have to divide them by 10 (if they have 2 digits), 100 (if they have 3 digits), etc ...

+1
source

The answer from @rabudde works well, but for those who need to handle negative numbers, the version is updated here:

 function intelliRound(num) { var len = (num + '').length; var result = 0; if (num < 0) { var fac = Math.pow(10, len - 2); result = Math.floor(num / fac) * fac; } else { var fac = Math.pow(10, len - 1); result = Math.ceil(num / fac) * fac; } return result; } alert(intelliRound(13)); alert(intelliRound(349)); alert(intelliRound(5645)); alert(intelliRound(-13)); alert(intelliRound(-349)); alert(intelliRound(-5645)); 
+1
source

The intelliRound function from the other answers works well, but I needed support for decimal numbers (e.g. 0.123, -0.987) and not numbers, so I used this function:

 /** * Function that returns the floor/ceil of a number, to an appropriate magnitude * @param {number} num - the number you want to round * * eg * magnitudeRound(0.13) => 1 * magnitudeRound(13) => 20 * magnitudeRound(349) => 400 * magnitudeRound(9645) => 10000 * magnitudeRound(-3645) => -4000 * magnitudeRound(-149) => -200 */ function magnitudeRound(num) { const isValidNumber = typeof num === 'number' && !Number.isNaN(num); const result = 0; if (!isValidNumber || num === 0) return result; const abs = Math.abs(num); const sign = Math.sign(num); if (abs > 0 && abs <= 1) return 1 * sign; // percentages on a scale -1 to 1 if (abs > 1 && abs <= 10) return 10 * sign; const zeroes = '${Math.round(abs)}'.length - 1; // eg 123 => 2, 4567 => 3 const exponent = 10 ** zeroes; // math floor and ceil only work on integer const roundingDirection = sign < 0 ? 'floor' : 'ceil'; return Math[roundingDirection](num / exponent) * exponent; } 
0
source

All Articles