-1

I want to limit the number to max 5 digits by rounding the decimal. If we have less than 5 digits the number should stay untouched. Could anyone help me to write that function?

function limitAndRoundNumber(number) {
 ...
}

Exemplary inputs and outputs:

limitAndRoundNumber(1.234) should return 1.234 (unchanged because number has less than 5 digits) 
limitAndRoundNumber(1.234567) should return 1.2346
limitAndRoundNumber(12.34567) should return 12.346
limitAndRoundNumber(123.4567) should return 123.46
limitAndRoundNumber(1234.567) should return 1234.6
limitAndRoundNumber(12345) should return 12345

Input number can be grater than 0 and less than 100000

Lukas
  • 37
  • 6
  • 2
    What should `123456` return? – evolutionxbox Jun 15 '22 at 11:52
  • 123456 is not valid input. – Lukas Jun 15 '22 at 11:54
  • Ok. You probably should consider what it does with invalid inputs. Try counting the digits after the decimal, and then using using [`.toFixed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)? You'll probably want to convert it back into a number – evolutionxbox Jun 15 '22 at 11:55
  • 1
    @evolutionxbox rather counts the digits _before_ the decimal, I would say? And then 5 minus that number, is the number of digits after the decimal, the precision that you want to round to. – CBroe Jun 15 '22 at 11:59
  • What should `0.123456` return? – Wyck Jun 15 '22 at 12:27
  • See the [earlier question](https://stackoverflow.com/questions/72622738/limit-the-length-of-number-by-rounding-it) by this user. – isherwood Jun 15 '22 at 14:01

2 Answers2

2

I tend to use the following to cut a number to a set number of decimals

Math.round(number * multiplier) / multiplier

Where 'multiplier' is 10 to the power of the number of decimals. Now you only need to figure out what the multiplier needs to be, and you can do that by rounding to a whole value and getting the string length of the number. So something like:

function limitAndRoundNumber(number) {
  const wholeDigits = Math.round(number).toString().length;
  const power = wholeDigits <= 5 ? 5 - wholeDigits : 0;
  const multiplier = 10**power;

  return Math.round(number * multiplier) / multiplier;
}

console.log(limitAndRoundNumber(1.23));
console.log(limitAndRoundNumber(1.234567));
console.log(limitAndRoundNumber(12.34567));
console.log(limitAndRoundNumber(123.4567));
console.log(limitAndRoundNumber(1234.567));
console.log(limitAndRoundNumber(12345));
console.log(limitAndRoundNumber(123456));
1

One solution is to convert the number to a string and use String.slice(0,requiredLength) to adjust the length before returning a number version of the string.

The length would depend on whether the number contained a decimal separator, which could be determined by a conditional within the function.

Working snippet:

function limitAndRoundNumber(number) {
const length = (number.toString().indexOf('.')) ? 6 : 5;

return parseFloat(number.toString().slice(0,length));
}

console.log(limitAndRoundNumber(1.234567));
console.log(limitAndRoundNumber(12.34567));
console.log(limitAndRoundNumber(123.4567));
console.log(limitAndRoundNumber(1234.567));
console.log(limitAndRoundNumber(12345));
console.log(limitAndRoundNumber(12));
console.log(limitAndRoundNumber(12.3));

The function could be modified to allow for any length, by including a length argument and referencing it in the ternary operator test for the decimal separator:

length = (number.toString().indexOf('.')) ? length+1 : length;
Dave Pritlove
  • 2,601
  • 3
  • 15
  • 14