Rand Stats

Intl::Token::Number

zef:guifa

Intl::Token::Number

A regex token for detecting localized numbers

To use, simply include the token <local-number> in anything interpreted as regex.

use Intl::Token::Number;

# (assuming English)
if '15,859,333.41' ~~ /<local-number>/ {
    say ~$<local-number>;  # 15,859,333.41   <-- stringified
    say +$<local-number>;  # 15859333.41     <-- numerical
}

The <local-number> token defaults to using the language provided by user-language. This makes it quite useful for parsing CLI. Sometimes, though, you'll want / need to specify the language directly. Just include it after a colon:

# Korean uses commas to separate thousands
'1,234' ~~ /<local-number: 'ko'>/;
say +$<local-number>; # 1234

# Spanish uses commas to separate integers from fractionals
'1,234' ~~ /<local-number: 'es'>/;
say +$<local-number>; # 1.234

Formats including percents, permilles, and exponential notation are supported:

# Assuming English
'90%' ~~ /<local-number>/;
say +$<local-number>; # 0.9

'1.23E3' ~~ /<local-number>/;
say +$<local-number>; # 123

For those interested in details about the match, the match object is introspectable:

To do

Version history