1

For example:

// Current behaviour
1e+25.toString() // Becomes "1e+25"

// Want this instead
1e+25.toDecimalString() // Becomes "1000000000..."

Dylan Kerler
  • 2,007
  • 1
  • 8
  • 23
  • `1e+25` is larger than [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER), it cannot be represented exactly as `Number`. You should use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) for values so large. – axiac Dec 27 '20 at 15:20
  • @axiac I don't want to represent as number. Only convert it to a string - There is no limit on strings. – Dylan Kerler Dec 27 '20 at 15:47
  • Convert it from what? From a number, isn't it? `1e+25` is a value of type `Number`. That is equal (due to the way JavaScript stores the numbers) with `1e+25 + 1` and with the next billion integer values. [Check it out on TIO](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/OT@vOD8nVS8nP13DMFXbyFTB1tZWAcLSBtKWmv//AwA). If you don't use `BigInt` your approach is invalid from the beginning and you will probably get incorrect and unexpected outcome at some point. – axiac Dec 27 '20 at 16:46

1 Answers1

2

you may use toLocaleString() method. try this:

1e+25.toLocaleString("en-US", { useGrouping: false })
// Output: "10000000000000000000000000"
Dylan Kerler
  • 2,007
  • 1
  • 8
  • 23
s.sohi
  • 128
  • 5
  • **It is not so good. This `toLocaleString` method lose precision**: `a = 712569312664357328695151392 + 8100824045303269669937; a.toLocaleString('en-US', { useGrouping: false })`. Any Idea? – Kasir Barati Jul 17 '22 at 19:23