How to convert a String containing Scientific Notation to correct Javascript number format

前端 未结 6 1116
深忆病人
深忆病人 2020-11-27 06:41

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

6条回答
  •  借酒劲吻你
    2020-11-27 07:09

    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(, [radix]) function:

    • ignores leading whitespaces (" 24" -> 24)
    • empty strings return NaN
    • first non-numeric char finishes parsing (" 42answer" -> 42)
    • under above rule, decimal places finish parsing as well

    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:

    • it parses first decimal point
    • ignores leading 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:

      • only numeric [0-9] chars, starts with numeric or + or - -> number (integer)
      • same, but contains floating-point -> number (floating-point)
      • contains hexadecimal -> number (integer)
      • empty string -> 0
      • all other cases -> 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

    1. ToNumber abstract operation in ECMAScript standard
    2. One of the best reads I had on this topic and many others: Matt Frisbie's "Professional Javascript for Web Developers".
    3. Type coercion explained (blog post)

提交回复
热议问题