1

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!)

Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
  • JavaScript numbers only have about 17 digits of precision. It can't represent a 300-digit number exactly. – Barmar Jul 17 '19 at 17:08
  • Have you considered [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)? – zero298 Jul 17 '19 at 17:08
  • Here is a function: https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript – Get Off My Lawn Jul 17 '19 at 17:09

1 Answers1

3

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)
Tom O.
  • 5,730
  • 2
  • 21
  • 35