Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support BigDecimal in number localization #240

Merged
merged 3 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/twitter_cldr/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# encoding: UTF-8
require "bigdecimal"

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

[Integer, Float].each do |klass|
[Integer, Float, BigDecimal].each do |klass|
TwitterCldr::Localized::LocalizedNumber.localize(klass)
end

Expand Down
19 changes: 15 additions & 4 deletions lib/twitter_cldr/formatters/numbers/number_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,35 @@ def parse_number(number, options = {})
precision = options[:precision] || precision_from(number)
rounding = options[:rounding] || 0

number = "%.#{precision}f" % round_to(number, precision, rounding).abs
if number.is_a? BigDecimal
number = precision == 0 ?
opoudjis marked this conversation as resolved.
Show resolved Hide resolved
round_to(number, precision, rounding).abs.fix.to_s("F") :
round_to(number, precision, rounding).abs.round(precision).to_s("F")
else
number = "%.#{precision}f" % round_to(number, precision, rounding).abs
end
number.split(".")
end

def round_to(number, precision, rounding = 0)
factor = 10 ** precision
result = (number * factor).round.to_f / factor
result = number.is_a?(BigDecimal) ?
((number * factor).fix / factor) :
((number * factor).round.to_f / factor)

if rounding > 0
rounding = rounding.to_f / factor
result = (result * (1.0 / rounding)).round.to_f / (1.0 / rounding)
result = number.is_a?(BigDecimal) ?
((result * (1.0 / rounding)).fix / (1.0 / rounding)) :
((result * (1.0 / rounding)).round.to_f / (1.0 / rounding))
end

result
end

def precision_from(num)
parts = num.to_s.split(".")
return 0 if num.is_a?(BigDecimal) && num.fix == num
parts = (num.is_a?(BigDecimal) ? num.to_s("F") : num.to_s ).split(".")
parts.size == 2 ? parts[1].size : 0
end

Expand Down
25 changes: 25 additions & 0 deletions spec/localized/localized_number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@
end
end

context 'big decimals' do
let(:integer) { described_class.new(BigDecimal("123456789012345678901234567890"), :en) }
let(:decimal) { described_class.new(BigDecimal("123456789012345678901234567890.123"), :en) }

it 'should default precision to zero' do
expect(integer.to_s).to eq("123,456,789,012,345,678,901,234,567,890")
end

it 'should not overwrite precision when explicitly passed' do
expect(integer.to_s(precision: 2)).to eq("123,456,789,012,345,678,901,234,567,890.00")
end

it 'should not truncate big decimal when precision set to zero (Ruby 3.0 issue)' do
expect(integer.to_s(precision: 0)).to eq("123,456,789,012,345,678,901,234,567,890")
end

it 'should default precision to that of the supplied number' do
expect(decimal.to_s).to eq("123,456,789,012,345,678,901,234,567,890.123")
end

it 'should not overwrite precision when explicitly passed' do
expect(decimal.to_s(precision: 2)).to eq("123,456,789,012,345,678,901,234,567,890.12")
end
end

context 'currencies' do
let(:number) { described_class.new(10, :en, type: :currency) }

Expand Down