Intl::Format::Number

A module for formatting numbers in a localized manner

Intl::Format::Number

A module for formatting numbers in a localized manner

To use, simply say

use Intl::Format::Number;

# Assuming user-language is 'en' (English)
say format-number 4834853;  # 4,834,853
say format-number 12.394;   # 12.394
say format-number 1/3;      # 0.333333333333333

# A separate call can get a number formatter which allows you to specify more options
my $formatter = local-number-formatter('en', :minimum-significant-digits<2>); 

It's that easy :-)

Currently supports percent, permille and scientific formatting in addition to the basic number format for languages with decimal number systems (sorry ancient Romans or Mayans!).

Other formatting styles (like compact) styles and options specified in TR 35 / CLDR, are NYI but will be available soon (current will be available via a separate module).

Performance is about an order of magnitude slower than .Str for most formatting, and I continue to try to optimize it farther. Balancing performance with the ability to, e.g., format a FatRat of several thousand digits, isn't easy :-)

Options

The Raku-defined variable $*TOLERANCE is used in the case of extremely long decimals (e.g. any repeating decimal like 1/3). Raku defaults it to 1e-15 (thus providing 15 digits of decimals) but you can set it to any number. You are highly advised not to set it to 0 as this may cause an infinite loop unless. (In the future, setting the maximum fractional digits to Inf and $*TOLERANCE to 0 will generate an error).

Basic options

  • :language<…>Sets the language to use for formatting. If the Unicode U Extension tag nu (for numbering system) is present, it will be respected.

  • :type<…> (standard, percent, permille, scientific, compact)Sets the type of number desired. If percent or permille is selected, the number is multiplied by 100 or 1000 prior to display (such that .123 becomes 12.3% or 123‰). Compact numbers are not yet supported.

  • :length<…>Sets the length to use for formatting. Defaults to standard, which is the only option available outside of compact numbers.

Number formatter options (for use with get-number-formatter)

optiondescriptionexample
maximum-integer-digitsNo more whole digits than thiswhen 2 1234 yields 34
minimum-integer-digitsPad with zeros to have at least this manywhen 412 yields 0012
maximum-fractional-digitsNo more decimal digits than thiswhen 21.2345 yields 1.23
minimum-fractional-digitsAdd trailing zeros to have at least this manywhen 31.2 yields 1.200
maximum-significant-digitsLimits the amount of significant digits usedwhen 21234 yields 1200
minimum-significant-digitsAdd trailing zeros if necessarywhen 31 yields 1.00
show-decimalForces the decimal to be shownwhen True1 yields 1.
show-signShows the sign regardless polaritywhen True1 yields +1
symbolsAdjusts the symbols used in formatting.
number-systemSet the digits used. Must be a CLDR code.when arab, 1234 yields ١٢٣٤
rastReturns a RakuAST node instead

If significant digits are specified, then the integral/fractional digits are currently ignored. Presently, the RakuAST node generated is a RakuAST::Sub. Additional documentation on it will be included in future updates to improve its integration with other modules.

Todo

  • Better documentation

  • More tests

  • Cleanup code

Version history

  • v0.4.0

    • Formally add support for local-number-formatter and number-formatter

  • v0.3.0

    • Added support for exponential formats (:type<exponential>)

    • Adjust some code for the newest version of RakuAST

  • v0.2.1

    • Added support for percent and permille formats (:type<percent> and :type<permille>)

    • Respects language tag's numering system (:language<en-u-nu-limb> will use Limbu numerals)

    • Fixed (mostly) a caching bug

  • v0.2.0

    • First version to use RakuAST (requires Rakudo 2022.12 or higher)

    • Initial support for negative numbers (should work for most all languages)

    • Initial support for formatting options

    • New and improved number format parser (faster, more accurate, and more maintainable!)

    • Added test file for grammar parsing

  • v0.1.1

    • Fix bug for n = 0, n = 10ˣ, and n < 0

  • v0.1.0

    • Initial version

License and Copyright

© 2020-2023 Matthew Stephen Stuckwisch. Licensed under the Artistic License 2.0.

Intl::Format::Number v0.4.0

A module for formatting numbers in a localized manner

Authors

  • Matthew Stephen Stuckwisch

License

Artistic-2.0

Dependencies

Intl::CLDR:auth<zef:guifa>:ver<0.7.2+>User::Language:auth<zef:guifa>:ver<0.5.1+>Intl::LanguageTag:auth<zef:guifa>:ver<0.12.3+>

Test Dependencies

Provides

  • Intl::Format::Number
  • Intl::Format::Number::Actions
  • Intl::Format::Number::Grammar

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.