money 3.5.3 → 3.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,14 @@
1
+ Money 3.5.4
2
+ ===========
3
+
4
+ Features
5
+ --------
6
+ - Added Currency#decimal_places.
7
+
8
+ Bugfixes
9
+ --------
10
+ - Fixed error with Money#to_s error with negative amounts that are only cents.
11
+
1
12
  Money 3.5.3
2
13
  ===========
3
14
 
@@ -237,6 +237,19 @@ class Money
237
237
  # @return [String]
238
238
  attr_reader :delimiter
239
239
 
240
+ # The number of decimal places needed.
241
+ #
242
+ # @return [Integer]
243
+ def decimal_places
244
+ if subunit_to_unit == 1
245
+ 0
246
+ elsif subunit_to_unit % 10 == 0
247
+ Math.log10(subunit_to_unit).to_s.to_i
248
+ else
249
+ Math.log10(subunit_to_unit).to_s.to_i+1
250
+ end
251
+ end
252
+
240
253
  # Create a new +Currency+ object.
241
254
  #
242
255
  # @param [String, Symbol, #to_s] id Used to look into +TABLE+ and retrieve
@@ -900,17 +900,13 @@ class Money
900
900
  # @example
901
901
  # Money.ca_dollar(100).to_s #=> "1.00"
902
902
  def to_s
903
- decimal_places = if currency.subunit_to_unit == 1
904
- 0
905
- elsif currency.subunit_to_unit % 10 == 0
906
- Math.log10(currency.subunit_to_unit).to_s.to_i
907
- else
908
- Math.log10(currency.subunit_to_unit).to_s.to_i+1
909
- end
910
903
  unit, subunit = cents.abs.divmod(currency.subunit_to_unit).map{|o| o.to_s}
911
- unit = (unit.to_i * -1).to_s if cents < 0
912
- return unit if decimal_places == 0
913
- subunit = (("0" * decimal_places) + subunit)[(-1*decimal_places)..-1]
904
+ if currency.decimal_places == 0
905
+ return "-#{unit}" if cents < 0
906
+ return unit
907
+ end
908
+ subunit = (("0" * currency.decimal_places) + subunit)[(-1*currency.decimal_places)..-1]
909
+ return "-#{unit}#{separator}#{subunit}" if cents < 0
914
910
  "#{unit}#{separator}#{subunit}"
915
911
  end
916
912
 
@@ -1099,15 +1095,14 @@ class Money
1099
1095
  # avoiding floating point arithmetic here to ensure accuracy
1100
1096
  cents = (major.to_i * currency.subunit_to_unit)
1101
1097
  # Because of an bug in JRuby, we can't just call #floor
1102
- decimal_places = Math.log10(currency.subunit_to_unit).to_s.to_i
1103
1098
  minor = minor.to_s
1104
- minor = if minor.size < decimal_places
1105
- (minor + ("0" * decimal_places))[0,decimal_places].to_i
1106
- elsif minor.size > decimal_places
1107
- if minor[decimal_places,1].to_i >= 5
1108
- minor[0,decimal_places].to_i+1
1099
+ minor = if minor.size < currency.decimal_places
1100
+ (minor + ("0" * currency.decimal_places))[0,currency.decimal_places].to_i
1101
+ elsif minor.size > currency.decimal_places
1102
+ if minor[currency.decimal_places,1].to_i >= 5
1103
+ minor[0,currency.decimal_places].to_i+1
1109
1104
  else
1110
- minor[0,decimal_places].to_i
1105
+ minor[0,currency.decimal_places].to_i
1111
1106
  end
1112
1107
  else
1113
1108
  minor.to_i
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
8
  - 5
9
- - 3
10
- version: 3.5.3
9
+ - 4
10
+ version: 3.5.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Luetke
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2010-12-15 00:00:00 -05:00
22
+ date: 2010-12-16 00:00:00 -05:00
23
23
  default_executable:
24
24
  dependencies:
25
25
  - !ruby/object:Gem::Dependency