I have a String e.g: \"4.874915326E7\"
. What is the best way to convert it to a javascript number format? (int or float)? if I try parseInt(), the E
You can also use +
sign in front of your string to get a number.
+"4.874915326E7" // == 48749153.26
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'
Using MathJs library worked best for me, tried a lot of these answers and none of them worked properly under certain circumstances. This worked perfectly. More at https://mathjs.org/
Solution, add MathJS and then call it like this:
<script src="https://cdn.jsdelivr.net/npm/mathjs@8.0.1/lib/browser/math.js" crossorigin="anonymous"></script>
function toPlainString(num) {
return math.format(num, {notation: 'fixed'});
}
Preamble
While other answers are sufficient and correct, to be able to correctly parse numbers from strings, it is useful to understand how type coercion (conversion in your case) works at least at high level.
Coercion rules
There are rules that define how conversion is performed when you invoke utilities like parseInt
, parseFloat
, Number
constructor or +
unary operator:
parseInt(<value>, [radix])
function:
" 24"
-> 24
)NaN
" 42answer"
-> 42
)The third rule is exactly why exponent part (ExponentPart
consists of ExponentIndicator
and SignedInteger
as defined in standard) is ignored - as soon as the e
char is encountered, parsing stops and the function returns the number parsed so far. In fact, it stops earlier - when the decimal point is first encountered (see the last rule).
parseFloat()
is identical to parseInt
, except:
0
(thus hexadecimal can't be parsed)As a rule of thumb, you should use parseInt
when converting to an "integer" and parseFloat
to "float" (note that they are actually the same type).
Number()
constructor and unary +
:
For boolean -> true
to 1
, false
to 0
For null
-> 0
(because null
is falsy)
For undefined
-> NaN
For numbers: pass-through
For strings:
+
or -
-> number (integer)0
NaN
For objects, their valueOf()
method is called. If result is NaN
, then toString()
method is called. Under the hood, the object is converted to primitive and that primitive is converted to number.
For symbols and BigInts -> TypeError
is thrown
Note on number format
As the question still attracts answers and comments that are concerned with formatting (as validly stated by the accepted answer), it should be stated that:
As applied to programming, there is a strict meaning of "number format":
representation of the numeric value"conversion" also has a strict meaning as type conversion (see standard)
ECMAScript implements double-precision 64-bit format and that's the only "format" it has. The question asks about converting a String to number format, therefore answers are expected to provide info on:
How to convert String to Number given the value represents a number in e-notation
References
ToNumber
abstract operation in ECMAScript standardThis 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.
Pass it as a string to the Number function.
Number("4.874915326E7") // returns 48749153.26
Number("4E27") // returns 4e+27
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"
Try something like this
Demo
Number("4.874915326E7").toPrecision()