human_number 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/human_number/formatters/number.rb +23 -19
- data/lib/human_number/rails/helpers.rb +15 -41
- data/lib/human_number/version.rb +1 -1
- data/lib/human_number.rb +32 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ab664232472a72c0114441a528ec9e72b1287d2cb0123d993b24324aeaa4f6f
|
4
|
+
data.tar.gz: '080aabbc217c4fc67ca01d4d9aa99791d8826e96bb83c2adb418ac09d7d6e27f'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1ad60597131895ef985c4f513d9f14842fa05b64310e947631dd1f34bdf5eb9c823cce3643534ffb06aa7c93015eec7118a5569c03d76263040c1127274d825
|
7
|
+
data.tar.gz: e12a574d465e336e25e95151dd007afe9a2065f8fec9652f7a59824fe1094120b75f5c8f715ae2d4948587037fbf5b162a6897cbec0753e6f6848879770133dc
|
@@ -34,7 +34,7 @@ module HumanNumber
|
|
34
34
|
#
|
35
35
|
# @api private
|
36
36
|
def format(number, locale:, abbr_units:, max_digits:, min_unit:, trim_zeros:)
|
37
|
-
locale = locale.to_sym
|
37
|
+
locale = (locale || I18n.locale).to_sym
|
38
38
|
|
39
39
|
# Direct delegation to appropriate system class
|
40
40
|
system_class = determine_system_class(locale)
|
@@ -48,6 +48,28 @@ module HumanNumber
|
|
48
48
|
)
|
49
49
|
end
|
50
50
|
|
51
|
+
# Formats a number using Rails' currency precision rules for the given currency.
|
52
|
+
# This ensures consistent precision regardless of display locale.
|
53
|
+
#
|
54
|
+
# @param number [Numeric] The number to format
|
55
|
+
# @param currency_code [String] ISO 4217 currency code
|
56
|
+
# @param locale [Symbol] Display locale (for context)
|
57
|
+
# @param options [Hash] Additional formatting options passed to number_to_currency
|
58
|
+
# @return [String] Formatted number with currency-appropriate precision
|
59
|
+
def format_with_currency_precision(number, currency_code:, locale:, **options)
|
60
|
+
locale = (locale || I18n.locale).to_sym
|
61
|
+
|
62
|
+
# Use currency's native locale for precision rules unless overridden
|
63
|
+
precision_locale = LocaleSupport.currency_precision_locale(currency_code, locale)
|
64
|
+
|
65
|
+
# Prepare options for number_to_currency
|
66
|
+
currency_options = options.dup
|
67
|
+
currency_options[:locale] = precision_locale
|
68
|
+
currency_options[:format] = "%n" # Always format as just the number (no currency symbol)
|
69
|
+
|
70
|
+
number_to_currency(number, **currency_options) || ZERO_STRING
|
71
|
+
end
|
72
|
+
|
51
73
|
# Default options for human number formatting
|
52
74
|
def default_options
|
53
75
|
{
|
@@ -68,24 +90,6 @@ module HumanNumber
|
|
68
90
|
}.freeze
|
69
91
|
end
|
70
92
|
|
71
|
-
# Formats a number using Rails' currency precision rules for the given currency.
|
72
|
-
# This ensures consistent precision regardless of display locale.
|
73
|
-
#
|
74
|
-
# @param number [Numeric] The number to format
|
75
|
-
# @param currency_code [String] ISO 4217 currency code
|
76
|
-
# @param locale [Symbol] Display locale (for context)
|
77
|
-
# @return [String] Formatted number with currency-appropriate precision
|
78
|
-
def format_with_currency_precision(number, currency_code:, locale:)
|
79
|
-
locale = locale.to_sym
|
80
|
-
|
81
|
-
# Use currency's native locale for precision rules
|
82
|
-
precision_locale = LocaleSupport.currency_precision_locale(currency_code, locale)
|
83
|
-
|
84
|
-
I18n.with_locale(precision_locale) do
|
85
|
-
number_to_currency(number, format: "%n", locale: precision_locale)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
93
|
private
|
90
94
|
|
91
95
|
def determine_system_class(locale)
|
@@ -7,75 +7,49 @@ module HumanNumber
|
|
7
7
|
# These helpers provide convenient access to HumanNumber functionality
|
8
8
|
# within Rails views and controllers with Rails-friendly parameter handling.
|
9
9
|
module Helpers
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# This is a Rails helper wrapper around HumanNumber.human_number that provides
|
13
|
-
# Rails-friendly parameter handling with optional locale detection.
|
10
|
+
# Rails helper for formatting numbers with intelligent abbreviations.
|
14
11
|
#
|
15
12
|
# @param number [Numeric] The number to format
|
16
13
|
# @param locale [Symbol, String, nil] The locale for formatting (default: current I18n locale)
|
17
14
|
# @param options [Hash] Additional formatting options
|
18
|
-
# @option options [Boolean] :abbr_units (true) Use abbreviated unit symbols
|
19
|
-
# @option options [Integer, nil] :max_digits (1) Maximum significant digits
|
20
|
-
# @option options [Integer, nil] :min_unit (nil) Minimum unit threshold
|
21
|
-
# @option options [Boolean] :trim_zeros (true) Remove trailing decimal zeros
|
22
|
-
#
|
23
|
-
# @return [String] The formatted number string
|
24
15
|
#
|
25
16
|
# @example Basic usage in views
|
26
|
-
# <%= human_number(1234567) %>
|
27
|
-
# <%= human_number(50000, locale: :ko) %>
|
28
|
-
# <%= human_number(1234567, max_digits: 2) %> #=> "1.23M"
|
17
|
+
# <%= human_number(1234567) %> #=> "1.2M"
|
18
|
+
# <%= human_number(50000, locale: :ko) %> #=> "5만"
|
29
19
|
#
|
30
|
-
# @see HumanNumber.human_number
|
20
|
+
# @see HumanNumber.human_number For detailed documentation and all available options
|
31
21
|
def human_number(number, locale: I18n.locale, **options)
|
32
22
|
HumanNumber.human_number(number, locale:, **options)
|
33
23
|
end
|
34
24
|
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# This helper provides Rails-friendly parameter handling for currency formatting
|
38
|
-
# with automatic locale detection and parameter normalization.
|
25
|
+
# Rails helper for formatting currency amounts with standard precision.
|
39
26
|
#
|
40
27
|
# @param number [Numeric] The amount to format
|
41
28
|
# @param currency_code [String] ISO 4217 currency code (e.g., 'USD', 'EUR')
|
42
29
|
# @param locale [Symbol, String, nil] Display locale (default: current I18n locale)
|
43
|
-
#
|
44
|
-
# @return [String] The formatted currency string
|
30
|
+
# @param options [Hash] Additional formatting options
|
45
31
|
#
|
46
32
|
# @example Basic usage in views
|
47
|
-
# <%= currency(1234.56, currency_code: 'USD') %>
|
48
|
-
# <%= currency(50000, currency_code: 'KRW'
|
49
|
-
# <%= currency(1234.99, currency_code: 'JPY') %> #=> "1,235円"
|
33
|
+
# <%= currency(1234.56, currency_code: 'USD') %> #=> "$1,234.56"
|
34
|
+
# <%= currency(50000, currency_code: 'KRW') %> #=> "50,000원"
|
50
35
|
#
|
51
|
-
# @see HumanNumber.currency
|
52
|
-
def currency(number, currency_code:, locale: I18n.locale)
|
53
|
-
HumanNumber.currency(number, currency_code:, locale
|
36
|
+
# @see HumanNumber.currency For detailed documentation and all available options
|
37
|
+
def currency(number, currency_code:, locale: I18n.locale, **options)
|
38
|
+
HumanNumber.currency(number, currency_code:, locale:, **options)
|
54
39
|
end
|
55
40
|
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# This helper combines human-readable number formatting with currency symbols,
|
59
|
-
# providing Rails-friendly access to abbreviated currency formatting.
|
41
|
+
# Rails helper for formatting currency amounts with intelligent abbreviations.
|
60
42
|
#
|
61
43
|
# @param number [Numeric] The amount to format
|
62
44
|
# @param currency_code [String] ISO 4217 currency code (e.g., 'USD', 'EUR')
|
63
45
|
# @param locale [Symbol, String, nil] Locale for formatting (default: current I18n locale)
|
64
46
|
# @param options [Hash] Additional formatting options
|
65
|
-
# @option options [Boolean] :abbr_units (true) Use abbreviated unit symbols
|
66
|
-
# @option options [Integer, nil] :max_digits (1) Maximum significant digits
|
67
|
-
# @option options [Integer, nil] :min_unit (nil) Minimum unit threshold
|
68
|
-
# @option options [Boolean] :trim_zeros (true) Remove trailing decimal zeros
|
69
|
-
#
|
70
|
-
# @return [String] The formatted currency string with abbreviations
|
71
47
|
#
|
72
48
|
# @example Basic usage in views
|
73
|
-
# <%= human_currency(1234567, currency_code: 'USD') %>
|
74
|
-
# <%= human_currency(50000, currency_code: 'KRW'
|
75
|
-
# <%= human_currency(1234567, currency_code: 'USD', max_digits: 2) %> #=> "$1.23M"
|
76
|
-
# <%= human_currency(1000000, currency_code: 'USD', abbr_units: false) %> #=> "$1 million"
|
49
|
+
# <%= human_currency(1234567, currency_code: 'USD') %> #=> "$1.2M"
|
50
|
+
# <%= human_currency(50000, currency_code: 'KRW') %> #=> "5만원"
|
77
51
|
#
|
78
|
-
# @see HumanNumber.human_currency
|
52
|
+
# @see HumanNumber.human_currency For detailed documentation and all available options
|
79
53
|
def human_currency(number, currency_code:, locale: I18n.locale, **options)
|
80
54
|
HumanNumber.human_currency(number, currency_code:, locale:, **options)
|
81
55
|
end
|
data/lib/human_number/version.rb
CHANGED
data/lib/human_number.rb
CHANGED
@@ -66,7 +66,6 @@ module HumanNumber
|
|
66
66
|
#
|
67
67
|
# @note Complete mode (max_digits: nil) shows full precision across multiple units
|
68
68
|
# @see Formatters::Number.format The underlying formatter implementation
|
69
|
-
# @since 1.0.0
|
70
69
|
def human_number(number, locale: I18n.locale, **options)
|
71
70
|
validate_number_input!(number)
|
72
71
|
validate_locale!(locale)
|
@@ -76,23 +75,41 @@ module HumanNumber
|
|
76
75
|
Formatters::Number.format(number, locale:, **final_options)
|
77
76
|
end
|
78
77
|
|
79
|
-
# Formats a currency amount
|
78
|
+
# Formats a currency amount with standard precision and symbol placement.
|
80
79
|
#
|
81
|
-
# This method
|
82
|
-
#
|
83
|
-
#
|
80
|
+
# This method provides currency formatting with automatic precision rules based on
|
81
|
+
# international standards, ensuring consistent display across different locales:
|
82
|
+
# - **Currency-specific precision**: 2 decimals for USD/EUR, 0 for JPY/KRW
|
83
|
+
# - **Native locale rules**: Precision determined by currency's origin locale
|
84
|
+
# - **Cross-locale consistency**: USD shows 2 decimals regardless of display locale
|
85
|
+
# - **Symbol placement**: Follows locale-specific conventions ($1,234 vs 1,234원)
|
84
86
|
#
|
85
87
|
# @param number [Numeric] The amount to format
|
86
88
|
# @param currency_code [String] ISO 4217 currency code (e.g., 'USD', 'EUR', 'KRW', 'JPY')
|
87
|
-
# @param locale [Symbol, String] Display locale for formatting rules
|
89
|
+
# @param locale [Symbol, String] Display locale for formatting rules (default: current I18n locale)
|
90
|
+
# Determines delimiter, separator, and symbol placement conventions
|
91
|
+
#
|
92
|
+
# @option options [Integer] :precision Decimal precision level. Overrides currency-specific precision
|
93
|
+
# @option options [Symbol] :round_mode Rounding mode (see BigDecimal.mode). Defaults to :default
|
94
|
+
# @option options [String] :separator Decimal separator. Defaults to locale-specific separator
|
95
|
+
# @option options [String] :delimiter Thousands delimiter. Defaults to locale-specific delimiter
|
96
|
+
# @option options [Boolean] :strip_insignificant_zeros (false) Remove trailing decimal zeros
|
97
|
+
#
|
98
|
+
# @note Only numeric formatting options are supported. Currency symbols and formats are
|
99
|
+
# automatically determined by ISO 4217 standards and locale conventions.
|
88
100
|
#
|
89
101
|
# @return [String] The formatted currency string
|
90
102
|
#
|
91
|
-
# @example
|
103
|
+
# @example Basic currency formatting
|
92
104
|
# HumanNumber.currency(1234.56, currency_code: 'USD', locale: :en) #=> "$1,234.56"
|
93
105
|
# HumanNumber.currency(50000, currency_code: 'KRW', locale: :ko) #=> "50,000원"
|
94
106
|
# HumanNumber.currency(1234.99, currency_code: 'JPY', locale: :ja) #=> "1,235円"
|
95
107
|
#
|
108
|
+
# @example Custom precision and formatting
|
109
|
+
# HumanNumber.currency(1234.56, currency_code: 'USD', precision: 1) #=> "$1,234.6"
|
110
|
+
# HumanNumber.currency(1234.56, currency_code: 'USD', delimiter: " ") #=> "$1 234.56"
|
111
|
+
# HumanNumber.currency(1234.50, currency_code: 'USD', strip_insignificant_zeros: true) #=> "$1,234.5"
|
112
|
+
#
|
96
113
|
# @example Cross-locale precision consistency
|
97
114
|
# # USD always shows 2 decimals regardless of display locale
|
98
115
|
# HumanNumber.currency(1234.56, currency_code: 'USD', locale: :ko) #=> "$1,234.56"
|
@@ -100,16 +117,20 @@ module HumanNumber
|
|
100
117
|
# # JPY always shows 0 decimals regardless of display locale
|
101
118
|
# HumanNumber.currency(1234.56, currency_code: 'JPY', locale: :en) #=> "1,235円"
|
102
119
|
#
|
103
|
-
# @
|
120
|
+
# @example Edge cases
|
121
|
+
# HumanNumber.currency(0, currency_code: 'USD') #=> "$0.00"
|
122
|
+
# HumanNumber.currency(-1234.56, currency_code: 'USD') #=> "-$1,234.56"
|
123
|
+
#
|
124
|
+
# @note Precision is determined by currency's native locale unless overridden via :precision option
|
125
|
+
# @see #human_currency For currency formatting with intelligent abbreviations (K/M/B)
|
104
126
|
# @see Formatters::Number.format_with_currency_precision Currency precision logic
|
105
127
|
# @see Formatters::Currency.format Currency symbol and format application
|
106
|
-
|
107
|
-
def currency(number, currency_code:, locale: I18n.locale)
|
128
|
+
def currency(number, currency_code:, locale: I18n.locale, **options)
|
108
129
|
validate_number_input!(number)
|
109
130
|
validate_currency_code!(currency_code)
|
110
131
|
validate_locale!(locale)
|
111
132
|
|
112
|
-
formatted_number = Formatters::Number.format_with_currency_precision(number, currency_code:, locale
|
133
|
+
formatted_number = Formatters::Number.format_with_currency_precision(number, currency_code:, locale:, **options)
|
113
134
|
Formatters::Currency.format(formatted_number, currency_code:, locale:)
|
114
135
|
end
|
115
136
|
|
@@ -153,7 +174,6 @@ module HumanNumber
|
|
153
174
|
# @note Combines human number formatting with currency-specific symbol placement
|
154
175
|
# @see #human_number For detailed number formatting options
|
155
176
|
# @see #currency For standard currency formatting without abbreviations
|
156
|
-
# @since 1.0.0
|
157
177
|
def human_currency(number, currency_code:, locale: I18n.locale, **options)
|
158
178
|
validate_number_input!(number)
|
159
179
|
validate_currency_code!(currency_code)
|