Class: HumanNumber::NumberSystem::DefaultSystem
- Inherits:
-
Object
- Object
- HumanNumber::NumberSystem::DefaultSystem
- Extended by:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/human_number/number_system.rb
Overview
Western/Default number formatting system using standard thousand-based units.
This system implements the standard Western number formatting convention using powers of 1000 progression: thousand, million, billion, trillion. It's used by most Western languages and global financial contexts.
Direct Known Subclasses
Defined Under Namespace
Classes: UnitBreakdown
Constant Summary collapse
- UNIT_DEFINITIONS =
[ { key: :trillion, divisor: 1_000_000_000_000 }, { key: :billion, divisor: 1_000_000_000 }, { key: :million, divisor: 1_000_000 }, { key: :thousand, divisor: 1_000 }, ].freeze
Class Method Summary collapse
-
.format_below_min_unit(number, locale) ⇒ Object
Format numbers below min_unit threshold with locale-appropriate delimiters.
- .format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, _min_unit) ⇒ Object
- .format_in_complete_mode(number, locale, abbr_units, _min_unit) ⇒ Object
- .format_number(number, locale:, abbr_units: true, min_unit: nil, max_digits: 2, trim_zeros: true) ⇒ Object
Class Method Details
.format_below_min_unit(number, locale) ⇒ Object
Format numbers below min_unit threshold with locale-appropriate delimiters
256 257 258 |
# File 'lib/human_number/number_system.rb', line 256 def format_below_min_unit(number, locale) number_with_delimiter(number, locale: locale) || number.to_s end |
.format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, _min_unit) ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/human_number/number_system.rb', line 247 def format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, _min_unit) # Abbreviated mode shows single largest unit: "1.2M" unit_breakdown = break_down_into_largest_unit(number) return nil if unit_breakdown.empty? format_abbreviated_unit_parts(unit_breakdown, locale, abbr_units, max_digits, trim_zeros) end |
.format_in_complete_mode(number, locale, abbr_units, _min_unit) ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/human_number/number_system.rb', line 239 def format_in_complete_mode(number, locale, abbr_units, _min_unit) # Complete mode shows all units: "1M 234K 567" unit_breakdown = break_down_into_all_units(number) return nil if unit_breakdown.empty? format_all_unit_parts(unit_breakdown, locale, abbr_units) end |
.format_number(number, locale:, abbr_units: true, min_unit: nil, max_digits: 2, trim_zeros: true) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/human_number/number_system.rb', line 222 def format_number(number, locale:, abbr_units: true, min_unit: nil, max_digits: 2, trim_zeros: true) return ZERO_STRING if number.zero? # Check if number meets minimum unit threshold for human formatting return format_below_min_unit(number, locale) if min_unit && number.abs < min_unit parts = if max_digits.nil? format_in_complete_mode(number, locale, abbr_units, min_unit) else format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, min_unit) end return number.to_s if parts.nil? finalize_result(number, parts) end |