shopify-money 0.14.5 → 0.14.6

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: 683fbe81ccbaba82b0ff6aeae0cd13086453cf72eef33c8be0fb8afa453f7b6a
4
- data.tar.gz: e4fdaff4a2761fd6d37a9ff50eeda4667572b563f9bd50e6c491808ab17fe3b5
3
+ metadata.gz: d1059640a2227392fc00f7292905bd4d4de2a71d075e8ffc1fb4d02944c5c8c2
4
+ data.tar.gz: 80db4d470ef80c27c09937b7fa146827d4183c519418a8382934e9a1875fd5f7
5
5
  SHA512:
6
- metadata.gz: 1190c9f7033aca4e0c44a60489dc2fac9b86d597afb6f3976da33b05e816719b8702af4b9c32ac0ffbf6230847c7858b18ce497c5d929336bbff8cfb89ee11fb
7
- data.tar.gz: 97bcf3f52c7985422490074cb8c920d6ccc85cfc64e401ad5fa4c7bb5f5ee9ad449a3d89b19bbf9f5afa0f79cf23a7cd61804497dfcfca02f6ce76863a7e5c16
6
+ metadata.gz: 2f8cf9b8ac4fafc4a372176ebe0e46d3b29c8fc82caf020f16732eef39d80ee3f35005faea7c9a96253791c9ce2c836776ca91dc31d55713bc577af55407eed2
7
+ data.tar.gz: d484b008600973688c52d232797e9ebb1efa6e782b33b8fa408853c08d35f96d8fde50c353ea53c9d9350378fd4f0390d579b619bef45b0f28d0fbe2d7e7c8b6
@@ -207,11 +207,22 @@ class Money
207
207
  end
208
208
 
209
209
  def to_s(style = nil)
210
- case style
210
+ units = case style
211
211
  when :legacy_dollars
212
- sprintf("%.2f", value)
212
+ 2
213
213
  when :amount, nil
214
- sprintf("%.#{currency.minor_units}f", value)
214
+ currency.minor_units
215
+ else
216
+ raise ArgumentError, "Unexpected style: #{style}"
217
+ end
218
+
219
+ rounded_value = value.round(units)
220
+ if units == 0
221
+ sprintf("%d", rounded_value)
222
+ else
223
+ sign = rounded_value < 0 ? '-' : ''
224
+ rounded_value = rounded_value.abs
225
+ sprintf("%s%d.%0#{units}d", sign, rounded_value.truncate, rounded_value.frac * (10 ** units))
215
226
  end
216
227
  end
217
228
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class Money
3
- VERSION = "0.14.5"
3
+ VERSION = "0.14.6"
4
4
  end
@@ -80,6 +80,33 @@ RSpec.describe "Money" do
80
80
  expect(non_fractional_money.to_s(:amount)).to eq("1")
81
81
  end
82
82
 
83
+ it "to_s correctly displays negative numbers" do
84
+ expect((-money).to_s).to eq("-1.00")
85
+ expect((-amount_money).to_s).to eq("-1.23")
86
+ expect((-non_fractional_money).to_s).to eq("-1")
87
+ expect((-Money.new("0.05")).to_s).to eq("-0.05")
88
+ end
89
+
90
+ it "to_s rounds when more fractions than currency allows" do
91
+ expect(Money.new("9.999", "USD").to_s).to eq("10.00")
92
+ expect(Money.new("9.889", "USD").to_s).to eq("9.89")
93
+ end
94
+
95
+ it "to_s does not round when fractions same as currency allows" do
96
+ expect(Money.new("1.25", "USD").to_s).to eq("1.25")
97
+ expect(Money.new("9.99", "USD").to_s).to eq("9.99")
98
+ expect(Money.new("9.999", "BHD").to_s).to eq("9.999")
99
+ end
100
+
101
+ it "to_s does not round if amount is larger than float allows" do
102
+ expect(Money.new("99999999999999.99", "USD").to_s).to eq("99999999999999.99")
103
+ expect(Money.new("999999999999999999.99", "USD").to_s).to eq("999999999999999999.99")
104
+ end
105
+
106
+ it "to_s raises ArgumentError on unsupported style" do
107
+ expect{ money.to_s(:some_weird_style) }.to raise_error(ArgumentError)
108
+ end
109
+
83
110
  it "as_json as a float with 2 decimal places" do
84
111
  expect(money.as_json).to eq("1.00")
85
112
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.5
4
+ version: 0.14.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-25 00:00:00.000000000 Z
11
+ date: 2020-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler