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 at the end is ignored.
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.
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
Number("4.874915326E7").toPrecision()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With