Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Add Indian number grouping for INR and Indian locales (#4680)

Shibu Ma5ebdca53735
diff --git a/lib/money/formatting.rb b/lib/money/formatting.rb
index 37e9efd729..36a1355122 100644
--- a/lib/money/formatting.rb
+++ b/lib/money/formatting.rb
@@ -8,28 +8,62 @@ module Money::Formatting
SPACE_DELIMITER_SYMBOL_AFTER = %i[pl nb hu].freeze
# European style: dot as thousands delimiter, comma as decimal separator, symbol before number
EUROPEAN_SYMBOL_BEFORE = %i[nl pt-BR].freeze
+ # Currencies and locales that use the South Asian numbering system (lakh/crore).
+ # Grouping is decided once in #format_options so every caller (web, exports,
+ # API formatted strings) agrees on it.
+ INDIAN_GROUPING_CURRENCIES = %w[INR].freeze
+ INDIAN_LOCALES = %i[en-IN en-IND hi hi-IN mr-IN ta te bn gu kn ml pa].freeze
+ INDIAN_DELIMITER_PATTERN = /(\d+?)(?=(\d\d)+(\d)(?!\d))/.freeze
FAST_FORMAT_KEYS = %i[unit precision delimiter separator format].freeze
DELIMITER_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/
def format(options = {})
- locale = options[:locale] || I18n.locale
- opts = format_options(locale).merge(options.except(:locale))
-
- fast_format(opts) || number_to_currency(amount, opts)
+ options = options.dup
+ requested_locale = options.delete(:locale)
+ locale = requested_locale || I18n.locale
+ indian_grouping = options.delete(:indian_grouping)
+ opts = format_options(locale, indian_grouping: indian_grouping).merge(options)
+
+ # Rails resolves number.format settings (strip_insignificant_zeros, ...)
+ # from the locale passed to number_to_currency. Bypass the fast path when
+ # the caller asked for a locale other than the active one so those settings
+ # come from the requested locale.
+ explicit_locale = requested_locale.present? && requested_locale.to_sym != I18n.locale.to_sym
+ fast = fast_format(opts) unless explicit_locale
+ return fast if fast
+
+ slow_opts = opts.merge(locale: locale)
+ # When the caller asked for a different locale, keep money at decimal
+ # precision instead of letting a locale such as hu round to significant
+ # digits. The ambient path keeps number_to_currency parity. A caller's
+ # own significant-digit option still wins.
+ slow_opts[:significant] = false if explicit_locale && !slow_opts.key?(:significant)
+ number_to_currency(amount, slow_opts)
end
alias_method :to_s, :format
- def format_options(locale = nil)
- local_option_overrides = locale_options(locale)
-
- {
+ def format_options(locale = nil, indian_grouping: nil)
+ opts = {
unit: get_symbol,
precision: currency.default_precision,
delimiter: currency.delimiter,
separator: currency.separator,
format: currency.default_format
- }.merge(local_option_overrides)
+ }.merge(locale_options(locale))
+
+ indian = indian_grouping?(locale, indian_grouping)
+ opts[:delimiter_pattern] = INDIAN_DELIMITER_PATTERN if indian
+
+ # Indian grouping and Indian locales always use Indian punctuation. A
+ # currency's own separators can otherwise produce mixed output such as
+ # "€12.34.567,00" or "12 34 567,00 €".
+ if indian || indian_locale?(locale)
+ opts[:delimiter] = ","
+ opts[:separator] = "."
+ end
+
+ opts
end
private
@@ -64,6 +98,18 @@ module Money::Formatting
format.gsub("%n", number_s).gsub("%u", unit)
end
+ # Explicit :indian_grouping wins; otherwise Indian grouping follows the
+ # currency (INR) or the active locale.
+ def indian_grouping?(locale, override)
+ return override unless override.nil?
+
+ INDIAN_GROUPING_CURRENCIES.include?(currency.iso_code) || indian_locale?(locale)
+ end
+
+ def indian_locale?(locale)
+ INDIAN_LOCALES.include?((locale || I18n.locale || :en).to_sym)
+ end
+
def get_symbol
if currency.symbol == "$" && currency.iso_code != "USD"
[ currency.iso_code.first(2), currency.symbol ].join
diff --git a/test/lib/money_test.rb b/test/lib/money_test.rb
index d19e41f674..297a16d793 100644
--- a/test/lib/money_test.rb
+++ b/test/lib/money_test.rb
@@ -216,6 +216,72 @@ class MoneyTest < ActiveSupport::TestCase
assert_equal "TW$1,000.12", Money.new(1000.12, :twd).format(locale: :"zh-TW")
end
+ test "formats INR amounts with the Indian numbering system" do
+ assert_equal "₹0.00", Money.new(0, :inr).format
+ assert_equal "₹50.00", Money.new(50, :inr).format
+ assert_equal "₹999.00", Money.new(999, :inr).format
+ assert_equal "₹1,000.00", Money.new(1000, :inr).format
+ assert_equal "₹10,000.00", Money.new(10_000, :inr).format
+ assert_equal "₹1,00,000.00", Money.new(100_000, :inr).format
+ assert_equal "₹1,00,000", Money.new(100_000, :inr).format(precision: 0)
+ assert_equal "₹12,34,567.89", Money.new(1_234_567.89, :inr).format
+ assert_equal "-₹12,34,567.89", Money.new(-1_234_567.89, :inr).format
+ assert_equal "₹1,00,00,000.00", Money.new(10_000_000, :inr).format
+ assert_equal "₹12,34,56,789.50", Money.new(123_456_789.5, :inr).format
+ end
+
+ test "formats with Indian grouping for Indian locales" do
+ assert_equal "₹12,34,567.89", Money.new(1234567.89, :inr).format(locale: :"en-IN")
+ assert_equal "₹12,34,567.89", Money.new(1234567.89, :inr).format(locale: :hi)
+ assert_equal "$1,00,000.00", Money.new(100_000, :usd).format(locale: :"en-IN")
+ assert_equal "$1,00,000.00", Money.new(100_000, :usd).format(locale: :hi)
+ end
+
+ test "uses Indian separators for non-INR currencies in Indian locales" do
+ assert_equal "€12,34,567.89", Money.new(1_234_567.89, :eur).format(locale: :"en-IN")
+ assert_equal "€12,34,567.89", Money.new(1_234_567.89, :eur).format(locale: :hi)
+ end
+
+ test "Indian grouping always uses Indian punctuation" do
+ assert_equal "12,34,567.89 ₹", Money.new(1_234_567.89, :inr).format(locale: :de)
+ assert_equal "12,34,567.89 ₹", Money.new(1_234_567.89, :inr).format(locale: :pl)
+ assert_equal "12,34,567.89 €", Money.new(1_234_567.89, :eur).format(locale: :de, indian_grouping: true)
+ assert_equal "12,34,567.89\u00A0€", Money.new(1_234_567.89, :eur).format(locale: :fr, indian_grouping: true)
+ assert_equal "€12,34,567.89", Money.new(1_234_567.89, :eur).format(locale: :ja, indian_grouping: true)
+ end
+
+ test "explicit locale applies that locale's zero stripping" do
+ assert_equal "1 000,5 $", I18n.with_locale(:en) { Money.new(1000.50, :usd).format(locale: :pl) }
+ assert_equal "1 000 $", I18n.with_locale(:en) { Money.new(1000.00, :usd).format(locale: :hu) }
+ end
+
+ test "explicit locale keeps decimal precision for money" do
+ assert_equal "1 000,12 $", I18n.with_locale(:en) { Money.new(1000.12, :usd).format(locale: :hu) }
+ end
+
+ test "explicit locale keeps the caller's significant-digit option" do
+ assert_equal "1.200 $", I18n.with_locale(:en) { Money.new(1234.56, :usd).format(locale: :de, precision: 2, significant: true) }
+ end
+
+ test "allows opting out of Indian grouping" do
+ assert_equal "₹100,000.00", Money.new(100_000, :inr).format(indian_grouping: false)
+ assert_equal "₹1,234,567.89", Money.new(1_234_567.89, :inr).format(indian_grouping: false)
+ assert_equal "$100,000.00", Money.new(100_000, :usd).format(locale: :"en-IN", indian_grouping: false)
+ end
+
+ test "Indian locale separators survive the grouping opt-out" do
+ assert_equal "€1,234,567.89", Money.new(1_234_567.89, :eur).format(locale: :"en-IN", indian_grouping: false)
+ assert_equal "€1,234,567.89", Money.new(1_234_567.89, :eur).format(locale: :hi, indian_grouping: false)
+ end
+
+ test "allows opting into Indian grouping for other currencies" do
+ assert_equal "$1,00,000.00", Money.new(100_000, :usd).format(indian_grouping: true)
+ end
+
+ test "explicit unit overrides keep Indian grouping" do
+ assert_equal "&#x20b9;1,00,000.00", Money.new(100_000, :inr).format(unit: "&#x20b9;")
+ end
+
test "formats correctly for Hungarian locale" do
# Hungarian uses space as thousands delimiter, comma as decimal separator, symbol after number
# HUF has 0 decimal places (no fillér)