routine val

Documentation for routine val assembled from the following types:

class Str

From Str

(Str) routine val

multi sub val(Str:D $MAYBEVAL:$val-or-fail)

Given a Str that may be parsable as a numeric value, it will attempt to construct the appropriate allomorph, returning one of IntStr, NumStr, RatStr or ComplexStr or a plain Str if a numeric value cannot be parsed. If the :val-or-fail adverb is provided it will return an X::Str::Numeric rather than the original string if it cannot parse the string as a number.

say val("42").^name;    # OUTPUT: «IntStr␤» 
say val("42e0").^name;  # OUTPUT: «NumStr␤» 
say val("42.0").^name;  # OUTPUT: «RatStr␤» 
say val("42+0i").^name# OUTPUT: «ComplexStr␤»

While characters belonging to the Unicode categories Nl (number letters) and No (other numbers) can be used as numeric literals in the language, they will not be converted to a number by val, by design. See unival if you need to convert such characters to Numeric.