Module: HumanNumber
- Extended by:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/human_number.rb,
lib/human_number/railtie.rb,
lib/human_number/version.rb,
lib/human_number/number_system.rb,
lib/human_number/rails/helpers.rb,
lib/human_number/locale_support.rb,
lib/human_number/formatters/number.rb,
lib/human_number/formatters/currency.rb
Defined Under Namespace
Modules: Formatters, LocaleSupport, NumberSystem, Rails Classes: Error, Railtie
Constant Summary collapse
- VERSION =
"0.2.2"- ZERO_STRING =
Constants for string literals and magic numbers used by system classes
"0"- EMPTY_SEPARATOR =
""- SPACE_SEPARATOR =
" "- DECIMAL_FORMAT_TEMPLATE =
"%.%df"- MINIMUM_UNIT_COUNT =
1.0- SINGLE_DIGIT_LIMIT =
1- I18N_DECIMAL_UNITS_KEY =
I18n key templates
"number.human.decimal_units"- I18N_ABBR_UNITS_SECTION =
"abbr_units"- I18N_UNITS_SECTION =
"units"
Class Method Summary collapse
-
.currency(number, currency_code:, locale: I18n.locale, **options) ⇒ String
Formats a currency amount with standard precision and symbol placement.
-
.currency_number_system(currency_code:) ⇒ Symbol
Determines the number system used for a given currency.
-
.human_currency(number, currency_code:, locale: I18n.locale, **options) ⇒ String
Formats a currency amount with intelligent, locale-aware abbreviations.
-
.human_number(number, locale: I18n.locale, **options) ⇒ String
Formats a number with intelligent, locale-aware abbreviations.
-
.number_system(locale: I18n.locale) ⇒ Symbol
Determines the number system used for a given locale.
Class Method Details
.currency(number, currency_code:, locale: I18n.locale, **options) ⇒ String
Only numeric formatting options are supported. Currency symbols and formats are automatically determined by ISO 4217 standards and locale conventions.
Precision is determined by currency's native locale unless overridden via :precision option
Formats a currency amount with standard precision and symbol placement.
This method provides currency formatting with automatic precision rules based on international standards, ensuring consistent display across different locales:
- Currency-specific precision: 2 decimals for USD/EUR, 0 for JPY/KRW
- Native locale rules: Precision determined by currency's origin locale
- Cross-locale consistency: USD shows 2 decimals regardless of display locale
- Symbol placement: Follows locale-specific conventions ($1,234 vs 1,234원)
123 124 125 126 127 128 129 130 |
# File 'lib/human_number.rb', line 123 def currency(number, currency_code:, locale: I18n.locale, **) validate_number_input!(number) validate_currency_code!(currency_code) validate_locale!(locale) formatted_number = Formatters::Number.format_with_currency_precision(number, currency_code:, locale:, **) Formatters::Currency.format(formatted_number, currency_code:, locale:) end |
.currency_number_system(currency_code:) ⇒ Symbol
Determines the number system used for a given currency.
This method identifies which cultural number system is typically used when formatting amounts in the specified currency, based on the currency's primary cultural and linguistic context.
242 243 244 245 |
# File 'lib/human_number.rb', line 242 def currency_number_system(currency_code:) validate_currency_code!(currency_code) NumberSystem.currency_number_system(currency_code:) end |
.human_currency(number, currency_code:, locale: I18n.locale, **options) ⇒ String
Combines human number formatting with currency-specific symbol placement
Formats a currency amount with intelligent, locale-aware abbreviations.
This method combines human-readable number formatting with currency symbols, providing culturally-appropriate abbreviations while maintaining currency symbol and format conventions.
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/human_number.rb', line 172 def human_currency(number, currency_code:, locale: I18n.locale, **) validate_number_input!(number) validate_currency_code!(currency_code) validate_locale!(locale) = Formatters::Number..merge() formatted_number = Formatters::Number.format(number, locale:, **) Formatters::Currency.format(formatted_number, currency_code:, locale:) end |
.human_number(number, locale: I18n.locale, **options) ⇒ String
Complete mode (max_digits: nil) shows full precision across multiple units
Formats a number with intelligent, locale-aware abbreviations.
This method provides culturally-appropriate number formatting with automatic unit system selection based on locale:
- Western locales: K/M/B/T (thousand/million/billion/trillion)
- East Asian locales: 만/억/조 or 万/億/兆
- Indian locales: thousand/lakh/crore
64 65 66 67 68 69 70 71 |
# File 'lib/human_number.rb', line 64 def human_number(number, locale: I18n.locale, **) validate_number_input!(number) validate_locale!(locale) = Formatters::Number..merge() Formatters::Number.format(number, locale:, **) end |
.number_system(locale: I18n.locale) ⇒ Symbol
Determines the number system used for a given locale.
This method identifies which cultural number system (Western, East Asian, or Indian) is used for formatting numbers in the specified locale.
207 208 209 210 |
# File 'lib/human_number.rb', line 207 def number_system(locale: I18n.locale) validate_locale!(locale) NumberSystem.number_system(locale:) end |