Expand/Shrink

to_number

Definition: object res = to_number(string s, object failure={}, integer inbase=10)
Description: Converts a string representation of a number into an atom.

s: A string such as "42".
failure: value to return should it not be possible to parse s to a number.
inbase: 2..62.

Returns the atom equivalent of s, or the specified failure value.
pwa/p2js: Supported.
Comments: Copes with signs, fractions, exponents, other number bases, and values outside 0..#3FFFFFFF.

The inbase parameter can be omitted/left as 10 for any s with an explict base, such as "0b01010" or "#DEADBEEF", since those would automatically override it to 2 and 16 respectively anyway, whereas should you fail to specify an inbase of 16 then obviously it will reject "DEADBEEF" since that is not a valid base 10 number. One caveat is that should you specify an inbase of 16, "0b01" and "0d10" would be interpreted as the hexadecimal #B01 and #D10, ie decimal 2817 and 3344, rather than 1 and 10 they’d get were inbase<=10, and in fact fail for bases 11 and 11..13 respectively, as they obviously should.

Note that unlike the simpler to_integer(), the empty string yields failure, and embedded underscores ('_') are permitted/skipped.
This routine also allows eg "65'A'" which is treated as if it was just "65" (but "66'A'" and "65'B'" would fail), specifically intended to help when processing ex.err files.

Also note that there is no equivalent of the return_bad_pos argument of the Euphoria version.
Example:
?to_number("12")                            -- 12
?to_number("-12")                           -- -12
?to_number("0b1100")                        -- 12
?to_number("1_000_000")                     -- 1000000
?to_number("13.25e-57")                     -- 13.25e-57
Implementation: See builtins\scanf.e (an autoinclude) for details of the actual implementation.