Class: ICU4X::NumberFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/icu4x/yard_docs.rb

Overview

Formats numbers according to locale-specific conventions.

NumberFormat supports decimal, percent, and currency formatting with configurable grouping, fraction digits, and rounding.

Examples:

Decimal formatting

formatter = ICU4X::NumberFormat.new(locale)
formatter.format(1234.56)  #=> "1,234.56" (in en-US)

Currency formatting

formatter = ICU4X::NumberFormat.new(locale, style: :currency, currency: "JPY")
formatter.format(1234)  #=> "¥1,234" (in ja-JP)

Percent formatting

formatter = ICU4X::NumberFormat.new(locale, style: :percent)
formatter.format(0.42)  #=> "42%"

Han decimal numerals via locale extension

locale = ICU4X::Locale.parse("ja-JP-u-nu-hanidec")
formatter = ICU4X::NumberFormat.new(locale, provider: provider)
formatter.format(1234)  #=> "一,二三四"

Instance Method Summary collapse

Constructor Details

#initialize(locale, provider: nil, style: :decimal, currency: nil, use_grouping: true, minimum_integer_digits: nil, minimum_fraction_digits: nil, maximum_fraction_digits: nil, rounding_mode: nil) ⇒ NumberFormat

Creates a new NumberFormat instance.

Examples:

formatter = ICU4X::NumberFormat.new(locale, minimum_fraction_digits: 2)

Parameters:

  • locale (Locale)

    the locale for formatting

  • provider (DataProvider, nil) (defaults to: nil)

    data provider (uses default if nil)

  • style (Symbol) (defaults to: :decimal)

    format style: ‘:decimal`, `:percent`, or `:currency`

  • currency (String, nil) (defaults to: nil)

    ISO 4217 currency code (required for ‘:currency` style)

  • use_grouping (Boolean) (defaults to: true)

    whether to use grouping separators

  • minimum_integer_digits (Integer, nil) (defaults to: nil)

    minimum number of integer digits

  • minimum_fraction_digits (Integer, nil) (defaults to: nil)

    minimum number of fraction digits

  • maximum_fraction_digits (Integer, nil) (defaults to: nil)

    maximum number of fraction digits

  • rounding_mode (Symbol, nil) (defaults to: nil)

    rounding mode for excess digits

Raises:

  • (DataError)

    if data for the locale is unavailable



465
466
467
468
# File 'lib/icu4x/yard_docs.rb', line 465

def initialize(locale, provider: nil, style: :decimal, currency: nil,
use_grouping: true, minimum_integer_digits: nil,
minimum_fraction_digits: nil, maximum_fraction_digits: nil,
rounding_mode: nil); end

Instance Method Details

#format(number) ⇒ String

Formats a number according to the configured options.

Examples:

formatter.format(1234567.89)  #=> "1,234,567.89"

Parameters:

  • number (Integer, Float, BigDecimal)

    the number to format

Returns:

  • (String)

    the formatted number string



478
# File 'lib/icu4x/yard_docs.rb', line 478

def format(number); end

#format_to_parts(number) ⇒ Array<FormattedPart>

Note:

For ‘style: :percent` and `style: :currency`, the current ICU4X experimental formatters do not provide part annotations. These styles return a single `:literal` part containing the entire formatted string.

Formats a number and returns an array of parts.

Each part contains a type and value, allowing for custom styling or processing of individual components.

Examples:

parts = formatter.format_to_parts(-1234.56)
# => [
#   #<ICU4X::FormattedPart type=:minus_sign value="-">,
#   #<ICU4X::FormattedPart type=:integer value="1,234">,
#   #<ICU4X::FormattedPart type=:decimal value=".">,
#   #<ICU4X::FormattedPart type=:fraction value="56">
# ]

Reconstruct the formatted string

parts.map(&:value).join  #=> "-1,234.56"

Parameters:

  • number (Integer, Float, BigDecimal)

    the number to format

Returns:



504
# File 'lib/icu4x/yard_docs.rb', line 504

def format_to_parts(number); end

#resolved_optionsHash

Returns the resolved options for this instance.

Returns:

  • (Hash)

    options hash with keys:

    • ‘:locale` [String] the resolved locale identifier

    • ‘:style` [Symbol] the format style

    • ‘:use_grouping` [Boolean] whether grouping is enabled

    • ‘:currency` [String] currency code (if applicable)

    • ‘:minimum_integer_digits` [Integer] minimum integer digits

    • ‘:minimum_fraction_digits` [Integer] minimum fraction digits

    • ‘:maximum_fraction_digits` [Integer] maximum fraction digits

    • ‘:rounding_mode` [Symbol] the rounding mode



518
# File 'lib/icu4x/yard_docs.rb', line 518

def resolved_options; end