class ICU::NumberFormatter

Overview

Number Formatter

This class provides a facility for formatting and parsing numbers.

Usage

nf = ICU::NumberFormatter.new("en_US")
nf.format(1234.56) # => "1,234.56"
nf.parse("1,234.56") # => 1234.56

See also

Defined in:

icu/number_formatter.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(locale : String = "en_US", format_style : FormatStyle = FormatStyle::Default) #

Creates a new NumberFormatter.

nf = ICU::NumberFormatter.new("en_US")
nf = ICU::NumberFormatter.new("fr_FR", ICU::NumberFormatter::FormatStyle::Currency)

[View source]

Instance Method Detail

def finalize #

[View source]
def format(number : Float64, currency_code : String) : String #

Formats a number with a currency code (3 letters code, see ISO 4217) into a string.

nf = ICU::NumberFormatter.new("en_US", ICU::NumberFormatter::FormatStyle::Currency)
nf.format(1234.56, "USD") # => "$1,234.56"

[View source]
def format(number : Float64) : String #
nf = ICU::NumberFormatter.new("en_US")
nf.format(1234.56) # => "1,234.56"

[View source]
def format(number : Int64) : String #

Formats a number into a string.

nf = ICU::NumberFormatter.new("en_US")
nf.format(1234) # => "1,234"

[View source]
def get_attribute(attribute : FormatAttribute) : Int32 #

Gets an attribute of the number formatter.

nf.get_attribute(ICU::NumberFormatter::FormatAttribute::GroupingUsed) # => 0

[View source]
def parse_float(text : String) : Float64 #

Parses a string into a floating point number.

nf = ICU::NumberFormatter.new("en_US")
nf.parse("1,234.56") # => 1234.56

[View source]
def parse_float_with_currency(text : String) : MonetaryAmount #

Parses a string into a monetary amount.

nf.parse_float_with_currency("$1,234.56") # => ICU::NumberFormatter::MonetaryAmount.new(1234.56, "USD")

[View source]
def parse_int(text : String) : Int64 #

Parses a string into an integer number.

nf = ICU::NumberFormatter.new("en_US")
nf.parse("1,234.56") # => 1234

[View source]
def set_attribute(attribute : FormatAttribute, value : Int32) #

Sets an attribute of the number formatter.

nf.set_attribute(ICU::NumberFormatter::FormatAttribute::GroupingUsed, 0)

[View source]