There's Number.toFixed, but it uses scientific notation if the number is >= 1e21 and has a maximum precision of 20. Other than that, you can roll your own, but it will be messy.

function toFixed(x) {
  if (Math.abs(x) < 1.0) {
    var e = parseInt(x.toString().split('e-')[1]);
    if (e) {
        x *= Math.pow(10,e-1);
        x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
    }
  } else {
    var e = parseInt(x.toString().split('+')[1]);
    if (e > 20) {
        e -= 20;
        x /= Math.pow(10,e);
        x += (new Array(e+1)).join('0');
    }
  }
  return x;
}

Above uses cheap-'n'-easy string repetition ((new Array(n+1)).join(str)). You could define String.prototype.repeat using Russian Peasant Multiplication and use that instead.

This answer should only be applied to the context of the question: displaying a large number without using scientific notation. For anything else, you should use a BigInt library, such as BigNumber, Leemon's BigInt, or BigInteger. Going forward, the new native BigInt (note: not Leemon's) should be available; Chromium and browsers based on it (Chrome, the new Edge [v79+], Brave) and Firefox all have support; Safari's support is underway.

Here's how you'd use BigInt for it: BigInt(n).toString()

Example:

const n = 13523563246234613317632;
console.log("toFixed (wrong): " + n.toFixed());
console.log("BigInt (right):  " + BigInt(n).toString());

Beware, though, that any integer you output as a JavaScript number (not a BigInt) that's more than 15-16 digits (specifically, greater than Number.MAX_SAFE_INTEGER + 1 [9,007,199,254,740,992]) may be be rounded, because JavaScript's number type (IEEE-754 double-precision floating point) can't precisely hold all integers beyond that point. As of Number.MAX_SAFE_INTEGER + 1 it's working in multiples of 2, so it can't hold odd numbers anymore (and similiarly, at 18,014,398,509,481,984 it starts working in multiples of 4, then 8, then 16, ...).

Consequently, if you can rely on BigInt support, output your number as a string you pass to the BigInt function:

const n = BigInt("YourNumberHere");

Example:

const n1 = BigInt(18014398509481985); // WRONG, will round to 18014398509481984
                                      // before `BigInt` sees it
console.log(n1.toString() + " <== WRONG");
const n2 = BigInt("18014398509481985"); // RIGHT, BigInt handles it
console.log(n2.toString() + " <== Right");

Answer from outis on Stack Overflow
Top answer
1 of 16
195

There's Number.toFixed, but it uses scientific notation if the number is >= 1e21 and has a maximum precision of 20. Other than that, you can roll your own, but it will be messy.

function toFixed(x) {
  if (Math.abs(x) < 1.0) {
    var e = parseInt(x.toString().split('e-')[1]);
    if (e) {
        x *= Math.pow(10,e-1);
        x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
    }
  } else {
    var e = parseInt(x.toString().split('+')[1]);
    if (e > 20) {
        e -= 20;
        x /= Math.pow(10,e);
        x += (new Array(e+1)).join('0');
    }
  }
  return x;
}

Above uses cheap-'n'-easy string repetition ((new Array(n+1)).join(str)). You could define String.prototype.repeat using Russian Peasant Multiplication and use that instead.

This answer should only be applied to the context of the question: displaying a large number without using scientific notation. For anything else, you should use a BigInt library, such as BigNumber, Leemon's BigInt, or BigInteger. Going forward, the new native BigInt (note: not Leemon's) should be available; Chromium and browsers based on it (Chrome, the new Edge [v79+], Brave) and Firefox all have support; Safari's support is underway.

Here's how you'd use BigInt for it: BigInt(n).toString()

Example:

const n = 13523563246234613317632;
console.log("toFixed (wrong): " + n.toFixed());
console.log("BigInt (right):  " + BigInt(n).toString());

Beware, though, that any integer you output as a JavaScript number (not a BigInt) that's more than 15-16 digits (specifically, greater than Number.MAX_SAFE_INTEGER + 1 [9,007,199,254,740,992]) may be be rounded, because JavaScript's number type (IEEE-754 double-precision floating point) can't precisely hold all integers beyond that point. As of Number.MAX_SAFE_INTEGER + 1 it's working in multiples of 2, so it can't hold odd numbers anymore (and similiarly, at 18,014,398,509,481,984 it starts working in multiples of 4, then 8, then 16, ...).

Consequently, if you can rely on BigInt support, output your number as a string you pass to the BigInt function:

const n = BigInt("YourNumberHere");

Example:

const n1 = BigInt(18014398509481985); // WRONG, will round to 18014398509481984
                                      // before `BigInt` sees it
console.log(n1.toString() + " <== WRONG");
const n2 = BigInt("18014398509481985"); // RIGHT, BigInt handles it
console.log(n2.toString() + " <== Right");

2 of 16
139

I know this is an older question, but shows recently active. MDN toLocaleString

const myNumb = 1000000000000000000000;
console.log( myNumb ); // 1e+21
console.log( myNumb.toLocaleString() ); // "1,000,000,000,000,000,000,000"
console.log( myNumb.toLocaleString('fullwide', {useGrouping:false}) ); // "1000000000000000000000"

you can use options to format the output.

Note:

Number.toLocaleString() rounds after 16 decimal places, so that...

const myNumb = 586084736227728377283728272309128120398;
console.log( myNumb.toLocaleString('fullwide', { useGrouping: false }) );

...returns...

586084736227728400000000000000000000000

This is perhaps undesirable if accuracy is important in the intended result.

🌐
GitHub
github.com › josdejong › mathjs › issues › 676
Formatting numbers without exponential notation (for bignumbers, also)? · Issue #676 · josdejong/mathjs
June 23, 2016 - I checked math.format, but, unfortunately, it copies the behavior of javascript Number.prototype.toFixed() , in the sense that specifying notation:"fixed"will always require an actual precision setting (if omitted, defaulted to zero), which is unfortunate, because it causes rounding and/or adding unnecessary 0's after the last significant digit after the decimal point. So, at the end of the day, it returns a different or non-canonical string representation of the input number.
Author   balagge
Discussions

Prevent Small Number from Converted to Scientific Notation
tbh I havent found any other way for very small numbers. its toFixed() all the way for displaying More on reddit.com
🌐 r/learnjavascript
5
9
November 7, 2019
Feature: Prevent toString from converting to scientific notation
Just like in regular javascript strings are shown with scientific notation over 20 digits. This prevents transferring these numbers to other systems like for blockchain and cyrptocurrency applicati... More on github.com
🌐 github.com
4
July 25, 2018
Scientific notation
Hello, I receive a scientific number and would need to have it as long string or number as just the numbers. I receive: 1.0442665000000301e+21 I want: 1044266500000030100000 How can I do this? More on community.make.com
🌐 community.make.com
1
1
September 4, 2023
javascript - convert big number to string without scientific notation - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
November 15, 2013
People also ask

Is there a native JavaScript method to convert scientific notation to a plain string
ANS: Currently, there isn’t a single built-in JavaScript method guaranteed to convert all scientific notations back to a plain string accurately without potential rounding. You often need to use custom functions, like those involving BigInt for integers, toLocaleString with specific options, or string manipulation with regular expressions.
🌐
sqlpey.com
sqlpey.com › javascript › display-large-numbers-without-scientific-notation
JavaScript: Display Large Numbers Without Scientific Notation
Why do my large numbers turn into scientific notation in JavaScript
ANS: JavaScript uses the IEEE-754 standard for numbers, which automatically switches to scientific notation (e.g., 1.23e+21) for very large or very small numbers to save space and simplify representation. This typically happens for numbers outside the approximate range of 1e-7 to 1e21.
🌐
sqlpey.com
sqlpey.com › javascript › display-large-numbers-without-scientific-notation
JavaScript: Display Large Numbers Without Scientific Notation
Can toLocaleString prevent scientific notation
ANS: Yes, toLocaleString() can display numbers without scientific notation by using options like useGrouping: false. However, it may still round very large or very small decimal numbers, so it’s not always a perfect solution for preserving extreme precision in floating-point numbers.
🌐
sqlpey.com
sqlpey.com › javascript › display-large-numbers-without-scientific-notation
JavaScript: Display Large Numbers Without Scientific Notation
🌐
Reddit
reddit.com › r/learnjavascript › prevent small number from converted to scientific notation
r/learnjavascript on Reddit: Prevent Small Number from Converted to Scientific Notation
November 7, 2019 -

I searched online but so far can't seem to find an answer to this. I have a very small number var a = 0.000000001 to be displayed to users, but it seems that JS always convert it to scientific notation. I would like to keep it as it, because later on I might add it with a normal number 1 + 0.000000000001 = 1.000000000001, and this time JS will display it as a 'normal' looking number.

Some say I could use number.toFixed() for the small number, but I wonder if there's a better way to tackle this issue, instead of always checking a number for scientific notation and running number.toFixed(). toFixed also makes it hard to add numbers because it's just a string.

🌐
GitHub
github.com › MikeMcl › decimal.js › issues › 105
Feature: Prevent toString from converting to scientific notation · Issue #105 · MikeMcl/decimal.js
July 25, 2018 - Just like in regular javascript strings are shown with scientific notation over 20 digits. This prevents transferring these numbers to other systems like for blockchain and cyrptocurrency applications.
Author   nickjuntilla
🌐
Stack Overflow
stackoverflow.com › questions › 20001325 › convert-big-number-to-string-without-scientific-notation
javascript - convert big number to string without scientific notation - Stack Overflow
November 15, 2013 - var n = Number.MAX_VALUE.toString(); var parts = n.split("e+"); var first = parts[0].replace('.', ""); var zeroes = parseInt(parts[1], 10) - (first.length - 1); for(var i = 0; i < zeroes; i++){ first += "0"; } // => first === "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
Find elsewhere
🌐
sqlpey
sqlpey.com › javascript › display-large-numbers-without-scientific-notation
JavaScript: Display Large Numbers Without Scientific Notation
January 28, 2017 - Consider using libraries like BigNumber.js or ensuring your numbers are handled as strings if the precision is critical for display or calculation. ANS: Currently, there isn’t a single built-in JavaScript method guaranteed to convert all scientific notations back to a plain string accurately without potential rounding.
🌐
Medium
medium.com › @anna7 › large-numbers-in-js-4feb6269d29b
Large numbers in JS. Hi everyone, | by Anna Ol | Medium
September 23, 2017 - Solution Because in this challenge you don’t need to make any arithmetic calculations with the number, you can just save it as a string and then do all manipulations.
🌐
W3Schools
w3schools.com › jsref › jsref_toexponential.asp
JavaScript toExponential() Method
decodeURI() decodeURIComponent() encodeURI() encodeURIComponent() escape() eval() Infinity isFinite() isNaN() NaN Number() parseFloat() parseInt() String() undefined unescape() JS Iterators
🌐
npm
npmjs.com › search
keywords:scientific notation - npm search
September 16, 2022 - Converts any big/small/precise decimal number represented as String, to exponential notation. ... stcruy• 1.0.2 • 7 years ago • 1 dependents • MITpublished version 1.0.2, 7 years ago1 dependents licensed under $MIT ... Convert a base-10 or scientific E-notation value to a decimal form string.
🌐
Educative
educative.io › answers › what-are-exponential-notation-and-nan-in-javascript
What are exponential notation and NaN in JavaScript?
November 4, 2002 - Note: parseInt is used to convert number string ("123") into a numeric value.
🌐
Math.js
mathjs.org › docs › reference › functions › format.html
math.js | an extensive math library for JavaScript and Node.js
September 23, 2017 - Specifies the maximum allowed length of the returned string. If it had been longer, the excess characters are deleted and replaced with '...'. callback: function A custom formatting function, invoked for all numeric elements in value, for example all elements of a matrix, or the real and imaginary parts of a complex number. This callback can be used to override the built-in numeric notation with any type of formatting.