Raku Land




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

Note: Presently, the <local-number> match object has several submatches. These may be interesting, but should not be relied upon at the moment. At some point in the future, a proper (and stable) method for accessing metadata will be provided.

To do

Version history