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 OverflowThere'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");
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.
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.
Edit:
This answer seems to be generating some confusion. The original question was asking how to convert scientific notation in the form of a string to a number (so that it could be used for calculation). However, a significant number of people finding this answer seem to think it's about converting a number that is being represented by javascript as scientific notation to a more presentable format. If that is in fact your goal (presentation), then you should be converting the number to a string instead. Note that this means you will not be able to use it in calculations as easily.
Original Answer:
Pass it as a string to the Number function.
Number("4.874915326E7") // returns 48749153.26
Number("4E27") // returns 4e+27
Converting a Number in Scientific Notation to a String:
This is best answered by another question, but from that question I personally like the solution that uses .toLocaleString(). Note that that particular solution doesn't work for negative numbers. For your convenience, here is an example:
(4e+27).toLocaleString('fullwide', {useGrouping:false}) // returns "4000000000000000000000000000"
I had a value like this 3.53048874968162e-09 and using Number.toFixed(20) worked for me:
value = new Number('3.53048874968162e-09')
//[Number: 3.53048874968162e-9]
value.toFixed(20)
//'0.00000000353048874968'
You can do something like this:
a = 200
a.toExponential(); //output 2e+2
fiddle: http://jsfiddle.net/Q8avJ/9/
At some point I wanted to use the coefficient and the exponent as numbers.
If you want to do that, you can use toExponential function, split the string and convert the items of the array to numbers.
In the following snippet I assign the numbers to the numInSciNot object and print them in the wanted format.
const num = 200;
const numInSciNot = {};
[numInSciNot.coefficient, numInSciNot.exponent] =
num.toExponential().split('e').map(item => Number(item));
console.log(`${numInSciNot.coefficient} x 10^${numInSciNot.exponent}`);
If you don't want to use them as numbers you can just use replace:
const num = 200;
console.log(num.toExponential().replace(/e\+?/, ' x 10^'));
In this snippet I've used RegExp to replace e or e+(in the case of positive exponent).
If you want to specify the number of digits after the decimal point, you can use toExponential(NumberOfDigits) in the above examples.
I have a node script that should output like this:
3656.90713699 3649.02909531 7.877241680000398
But the output is like this:
3.65690713699e-9 3.64902989531e-9 7.877241680000398e-12
How can I stop it from using the e-9?
EDIT:
I solved it in a very simple way, I just multiplied the output variables by 10^12:
myVar * 10 ** 12;