Search code examples
javascriptnumber-formatting

Javascript - get very big number as string containing all digits


I've consulted the mozilla developer reference for Number.prototype.toFixed, and it lead me to believe that if I have a very big number showing in scientific notation, I can do this to see the whole number:

myBigNum.toFixed(2);

Unfortunately this doesn't work. For example, if I want to see all 308 glorious digits of Number.MAX_VALUE, the following still shows me a string in scientific notation:

console.log(Number.MAX_VALUE.toFixed(2));
console.log(Number.MAX_VALUE.toFixed(2).constructor.name);

How can I get a string showing all the digits of an arbitrary, very large (or very small) number in javascript?

(Note: I realize that many trailing digits may exhibit float-point-funkiness!)


Solution

  • You could use toLocaleString method (with some options, if needed):

    const s = Number.MAX_VALUE.toLocaleString(undefined, {
      style: 'decimal',
      useGrouping: false //Flip to true if you want to include commas
    });
    
    console.log(s)