Class: Plurimath::NumberFormatter
- Inherits:
-
Object
- Object
- Plurimath::NumberFormatter
- Defined in:
- lib/plurimath/number_formatter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#localize_number ⇒ Object
Returns the value of attribute localize_number.
-
#localizer_symbols ⇒ Object
Returns the value of attribute localizer_symbols.
-
#precision ⇒ Object
Returns the value of attribute precision.
Instance Method Summary collapse
-
#format_number(_formula, number, format: {}) ⇒ Object
Formula-aware entry point.
- #formatted_number(number_string, locale: @locale, precision: @precision, format: {}) ⇒ Object
-
#initialize(locale = "en", localize_number: nil, localizer_symbols: {}, precision: nil) ⇒ NumberFormatter
constructor
A new instance of NumberFormatter.
- #localized_number(number_string, locale: @locale, precision: @precision, format: {}) ⇒ Object
- #twitter_cldr_reader(locale: @locale) ⇒ Object
Constructor Details
#initialize(locale = "en", localize_number: nil, localizer_symbols: {}, precision: nil) ⇒ NumberFormatter
Returns a new instance of NumberFormatter.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/plurimath/number_formatter.rb', line 7 def initialize( locale = "en", localize_number: nil, localizer_symbols: {}, precision: nil ) @locale = supported_locale(locale) @localize_number = localize_number @localizer_symbols = localizer_symbols @precision = precision end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
5 6 7 |
# File 'lib/plurimath/number_formatter.rb', line 5 def locale @locale end |
#localize_number ⇒ Object
Returns the value of attribute localize_number.
5 6 7 |
# File 'lib/plurimath/number_formatter.rb', line 5 def localize_number @localize_number end |
#localizer_symbols ⇒ Object
Returns the value of attribute localizer_symbols.
5 6 7 |
# File 'lib/plurimath/number_formatter.rb', line 5 def localizer_symbols @localizer_symbols end |
#precision ⇒ Object
Returns the value of attribute precision.
5 6 7 |
# File 'lib/plurimath/number_formatter.rb', line 5 def precision @precision end |
Instance Method Details
#format_number(_formula, number, format: {}) ⇒ Object
Formula-aware entry point. Returns a FormattedNumber, FormattedNotation, or String. Override in subclasses that need the Formula/Number context for rendering decisions; the default delegates to #formatted_number with the merged format hash.
57 58 59 |
# File 'lib/plurimath/number_formatter.rb', line 57 def format_number(_formula, number, format: {}) formatted_number(number.value.to_s, format: format) end |
#formatted_number(number_string, locale: @locale, precision: @precision, format: {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/plurimath/number_formatter.rb', line 34 def formatted_number( number_string, locale: @locale, precision: @precision, format: {} ) locale = supported_locale(locale) format = validated_format(format) base = format.fetch(:base, Formatter::Numbers::Base::DEFAULT_BASE) source = Formatter::Numbers::Source.new(number_string, base: base) = (source, locale, precision, format) if .notation_supported? return notation_renderer().render(source, .notation) end render_localized_number(source, ) end |
#localized_number(number_string, locale: @locale, precision: @precision, format: {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/plurimath/number_formatter.rb', line 19 def localized_number( number_string, locale: @locale, precision: @precision, format: {} ) result = formatted_number( number_string, locale: locale, precision: precision, format: format, ) result.to_s end |
#twitter_cldr_reader(locale: @locale) ⇒ Object
61 62 63 |
# File 'lib/plurimath/number_formatter.rb', line 61 def twitter_cldr_reader(locale: @locale) symbols_for(supported_locale(locale), {}) end |