twitter_cldr 6.5.0 → 6.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 837a383731a20ebacea14090a6189664506b5c76dd3da585e69566d314f17821
4
- data.tar.gz: 17f2ef911ce4d96d87427cf8c9cf44d4c1c408eef150cb0bb893880575e9350b
3
+ metadata.gz: 7840c80b8cc6bcf117de49198d83cdb18a938c57339f0facbef2bc67cef6a7c8
4
+ data.tar.gz: 8c92bead9ddba30899f7478dbcb94bed2d43cecb67fa69a5e565daff7930a02f
5
5
  SHA512:
6
- metadata.gz: ef45245e3218fedfc48185bb5495545049aa1774bf5ae68f2482433ba5411bc3d0a50734a8fafb964a289696f3fb2517aefd1dcd0eca873f3677181dfc6cace3
7
- data.tar.gz: 3a448cc613123e355db6f218b06850ee599868f612e2a9e99ccc39668d45446d14a399660e8ab928f41c76c37a0a63112e182cc294e7e647c525e99116aa921a
6
+ metadata.gz: 144647b4f36e6ec2df0394978a0f975bcbfcc62f0799cf1080e9c058fcc6daea90d9ad5f2270c080f1422200abd34874ac44e92935783c16a4cfea22a6e3bfdd
7
+ data.tar.gz: 60a03fcc538428dbc767351fb0a2370e3bd85da5e3b239f3529e074730b9ae0c7f918f849753fbad5a9fce902982fe52aff0d1943919389b674f29bf09950ace
@@ -1,9 +1,10 @@
1
1
  # encoding: UTF-8
2
+ require "bigdecimal"
2
3
 
3
4
  # Copyright 2012 Twitter, Inc
4
5
  # http://www.apache.org/licenses/LICENSE-2.0
5
6
 
6
- [Integer, Float].each do |klass|
7
+ [Integer, Float, BigDecimal].each do |klass|
7
8
  TwitterCldr::Localized::LocalizedNumber.localize(klass)
8
9
  end
9
10
 
@@ -69,24 +69,35 @@ module TwitterCldr
69
69
  precision = options[:precision] || precision_from(number)
70
70
  rounding = options[:rounding] || 0
71
71
 
72
- number = "%.#{precision}f" % round_to(number, precision, rounding).abs
72
+ if number.is_a? BigDecimal
73
+ number = precision == 0 ?
74
+ round_to(number, precision, rounding).abs.fix.to_s("F") :
75
+ round_to(number, precision, rounding).abs.round(precision).to_s("F")
76
+ else
77
+ number = "%.#{precision}f" % round_to(number, precision, rounding).abs
78
+ end
73
79
  number.split(".")
74
80
  end
75
81
 
76
82
  def round_to(number, precision, rounding = 0)
77
83
  factor = 10 ** precision
78
- result = (number * factor).round.to_f / factor
84
+ result = number.is_a?(BigDecimal) ?
85
+ ((number * factor).fix / factor) :
86
+ ((number * factor).round.to_f / factor)
79
87
 
80
88
  if rounding > 0
81
89
  rounding = rounding.to_f / factor
82
- result = (result * (1.0 / rounding)).round.to_f / (1.0 / rounding)
90
+ result = number.is_a?(BigDecimal) ?
91
+ ((result * (1.0 / rounding)).fix / (1.0 / rounding)) :
92
+ ((result * (1.0 / rounding)).round.to_f / (1.0 / rounding))
83
93
  end
84
94
 
85
95
  result
86
96
  end
87
97
 
88
98
  def precision_from(num)
89
- parts = num.to_s.split(".")
99
+ return 0 if num.is_a?(BigDecimal) && num.fix == num
100
+ parts = (num.is_a?(BigDecimal) ? num.to_s("F") : num.to_s ).split(".")
90
101
  parts.size == 2 ? parts[1].size : 0
91
102
  end
92
103
 
@@ -4,5 +4,5 @@
4
4
  # http://www.apache.org/licenses/LICENSE-2.0
5
5
 
6
6
  module TwitterCldr
7
- VERSION = '6.5.0'
7
+ VERSION = '6.6.0'
8
8
  end
@@ -100,6 +100,31 @@ describe TwitterCldr::Localized::LocalizedNumber do
100
100
  end
101
101
  end
102
102
 
103
+ context 'big decimals' do
104
+ let(:integer) { described_class.new(BigDecimal("123456789012345678901234567890"), :en) }
105
+ let(:decimal) { described_class.new(BigDecimal("123456789012345678901234567890.123"), :en) }
106
+
107
+ it 'should default precision to zero' do
108
+ expect(integer.to_s).to eq("123,456,789,012,345,678,901,234,567,890")
109
+ end
110
+
111
+ it 'should not overwrite precision when explicitly passed' do
112
+ expect(integer.to_s(precision: 2)).to eq("123,456,789,012,345,678,901,234,567,890.00")
113
+ end
114
+
115
+ it 'should not truncate big decimal when precision set to zero (Ruby 3.0 issue)' do
116
+ expect(integer.to_s(precision: 0)).to eq("123,456,789,012,345,678,901,234,567,890")
117
+ end
118
+
119
+ it 'should default precision to that of the supplied number' do
120
+ expect(decimal.to_s).to eq("123,456,789,012,345,678,901,234,567,890.123")
121
+ end
122
+
123
+ it 'should not overwrite precision when explicitly passed' do
124
+ expect(decimal.to_s(precision: 2)).to eq("123,456,789,012,345,678,901,234,567,890.12")
125
+ end
126
+ end
127
+
103
128
  context 'currencies' do
104
129
  let(:number) { described_class.new(10, :en, type: :currency) }
105
130
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_cldr
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.5.0
4
+ version: 6.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-23 00:00:00.000000000 Z
11
+ date: 2021-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: camertron-eprun