Search code examples
javascriptfunctionnumber-formatting

How to create a function in JavaScript takes numeral with digits instead of separators?


I want to create a function that takes a numeral (just digits without separators, e.g. 19093) and returns the standard way of reading a number, complete with punctuation.

what i tried to do

function sayNumber(num) {
  return num.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}


function test(num, expect) {
  const result = sayNumber(num);
  const pass = result === expect;
  console.log(`${pass ? "✓" : "ERROR ====>"} ${num} => ${result}`);
  return pass;
}

let failures = 0;
failures += !test(0, "0");
failures += !test(100, "100");
failures += !test(1000, "1,000");
failures += !test(10000, "10,000");
failures += !test(100000, "100,000");
failures += !test(1000000, "1,000,000");
failures += !test(10000000, "10,000,000");
if (failures) {
  console.log(`${failures} test(s) failed`);
} else {
  console.log("All tests passed");
}

newested changes and error messages when compiled.

function sayNumber(BigInt) {
  return new Intl.NumberFormat('en-US').format(BigInt);
}

function test(BigInt, expect) {
  const result = sayNumber(BigInt);
  const pass = result === expect;
  console.log(`${pass ? "✓" : "ERROR ====>"} ${BigInt} => ${result}`);
  return pass;
}

let failures = 0;
failures += !test(0, "0");
failures += !test(11, "11");
failures += !test(14, "14");
failures += !test(15, "15");

if (failures) {
  console.log(`${failures} test(s) failed`);
} else {
  console.log("All tests passed");
}

Error messages

✓ 0 => 0
✓ 11 => 11
✓ 14 => 14
✓ 15 => 15
All tests passed
FAILED: Expected: 'Zero.', instead got: '0'
FAILED: Expected: 'Eleven.', instead got: '11'
FAILED: Expected: 'Fourteen.', instead got: '14'
FAILED: Expected: 'Fifteen.', instead got: '15'
FAILED: Expected: 'Forty-three.', instead got: '43'
FAILED: Expected: 'Fifty.', instead got: '50'
FAILED: Expected: 'One thousand and one.', instead got: '1,001'
FAILED:

The expected values are the ones the IDE needs to read and expects, currently its not doing that either.


Solution

  • You need to use the BigInt class. Renaming the parameter from num to BigInt doesn't cause it to be a big integer.

    You can write a BigInt literal by putting n at the end of the number.

    function sayNumber(num) {
      return BigInt(num).toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
    }
    
    
    function test(num, expect) {
      const result = sayNumber(num);
      const pass = result === expect;
      console.log(`${pass ? "✓" : "ERROR ====>"} ${num} => ${result}`);
      return pass;
    }
    
    let failures = 0;
    failures += !test(0, "0");
    failures += !test(100, "100");
    failures += !test(1000, "1,000");
    failures += !test(10000, "10,000");
    failures += !test(100000, "100,000");
    failures += !test(1000000, "1,000,000");
    failures += !test(10000000, "10,000,000");
    failures += !test(999999999999999n, "999,999,999,999,999");
    if (failures) {
      console.log(`${failures} test(s) failed`);
    } else {
      console.log("All tests passed");
    }