to_num(str )

Converts a string to a number.

to_num returns an integer or a float depending on the presence of a decimal point in the input string. Only ’.’ is recognized as decimal point independent of locale settings.

Valid strings can contains optional leading white space(s) followed by an optional plus (’+’) or minus sign (’-’) and then one of the following:

(i)    A decimal number consisting of a sequence of decimal digits (e.g. 10, -5), an optional ’.’ to indicate a float number (e.g. 1.5234, -2.0, .36) and a optional decimal exponent that indicates multiplication by a power of 10 (e.g. 10e3, 2.5E-5, -5e-4).

(ii)    A hexadecimal number consisting of "0x" or "0X" followed by a nonempty sequence of hexadecimal digits (e.g. "0X3A", "0xb5").

(iii)    An infinity (either "INF" or "INFINITY", case insensitive)

(iv)    A Not-a-Number ("NAN", case insensitive)

Runtime exceptions are raised if the source string doesn’t contain a valid number or the result is out of range for the resulting type.

Parameters

str: string to convert

Return Value

Integer or float number according to the input string.

Example command:

  • to_num("10")
    • returns 10 //integer
  • to_num("3.14")
    • returns 3.14 //float
  • to_num("-3.0e5")
    • returns -3.0e5 //float due to ’.’ in the input string
  • to_num("+5.")
    • returns 5.0 //float due to ’.’ in the input string
  • to_num("123abc")
    • error string doesn’t contain a valid number