parse(T::Type, str, base=Int)

parse(type, str, [base])

Parse a string as a number. If the type is an integer type, then a base can be specified (the default is 10). If the type is a floating point type, the string is parsed as a decimal floating point number. If the string does not contain a valid number, an error is raised.

Examples

  1. Parse a simple mathematical expression:

    julia> parse("2 + 3")
    :(2 + 3)

    This example parses the expression "2 + 3" and returns an expression object that represents the addition operation.

  2. Parse a more complex expression with variables:

    julia> parse("x^2 + 2 * y - z")
    :((x ^ 2) + (2 * y) - z)

    It parses the expression "x^2 + 2 * y - z" and returns an expression object with variables x, y, and z.

  3. Handle syntax errors:

    julia> parse("3 + ")
    ERROR: syntax: expected expression

    In this example, the expression "3 + " is invalid, and parse raises a syntax error.

  4. Disable error raising upon evaluation:
    julia> parse("5 / 0", raise=false)
    :((5 / 0))

    By passing raise=false as an argument, parse will return an expression even if it contains an error. In this case, division by zero.

Common mistake example:

julia> parse("x + y")
:((x + y))

In this example, the variables x and y are not defined, so evaluating the parsed expression will raise an error. Make sure to define any variables used in the parsed expression beforehand.

See Also

base, big, bytes2hex, cconvert, complex, convert, dec, hex, hex2bytes, hex2num, oct, oftype, parse, promote, signed, unsafe_convert, unsigned, widen,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: