contains hexadecimal -> number (integer)įor objects, their valueOf() method is called.same, but contains floating-point -> number (floating-point).only numeric chars, starts with numeric or + or -> number (integer).ignores leading 0 (thus hexadecimal can't be parsed)Īs 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).ParseFloat() is identical to parseInt, except:
In fact, it stops earlier - when the decimal point is first encountered (see the last rule). 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. under above rule, decimal places finish parsing as well.first non-numeric char finishes parsing ( " 42answer" -> 42).ignores leading whitespaces ( " 24" -> 24).There are rules that define how conversion is performed when you invoke utilities like parseInt, parseFloat, Number constructor or + unary operator: 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. Note that that particular solution doesn't work for negative numbers. This is best answered by another question, but from that question I personally like the solution that uses. Number("4.874915326E7") // returns 48749153.26Ĭonverting a Number in Scientific Notation to a String: Pass it as a string to the Number function. Note that this means you will not be able to use it in calculations as easily. If that is in fact your goal (presentation), then you should be converting the number to a string instead. 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.
#Scientific notation converter how to#
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).
This answer seems to be generating some confusion.