ShadowBelmolve-money 2.3.7 → 2.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/money/core_extensions.rb +19 -15
- data/money.gemspec +1 -1
- metadata +1 -1
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.3.
|
|
1
|
+
2.3.8
|
|
@@ -10,7 +10,7 @@ class Numeric
|
|
|
10
10
|
if self.is_a? Integer
|
|
11
11
|
Money.new(self)
|
|
12
12
|
else
|
|
13
|
-
Money.new(self.to_s.gsub(/\./,'').
|
|
13
|
+
Money.new(self.to_s.gsub(/\./,'').to_i)
|
|
14
14
|
end
|
|
15
15
|
else
|
|
16
16
|
Money.new((self * 100).round)
|
|
@@ -28,17 +28,17 @@ class String
|
|
|
28
28
|
# 'USD 100'.to_money # => #<Money @cents=10000, @currency="USD">
|
|
29
29
|
# '$100 USD'.to_money # => #<Money @cents=10000, @currency="USD">
|
|
30
30
|
# 'hello 2000 world'.to_money # => #<Money @cents=200000 @currency="USD")>
|
|
31
|
-
def to_money
|
|
31
|
+
def to_money(with_cents = false)
|
|
32
32
|
# Get the currency.
|
|
33
33
|
matches = scan /([A-Z]{2,3})/
|
|
34
34
|
currency = matches[0] ? matches[0][0] : Money.default_currency
|
|
35
|
-
cents = calculate_cents(self)
|
|
35
|
+
cents = calculate_cents(self, with_cents)
|
|
36
36
|
Money.new(cents, currency)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
private
|
|
40
40
|
|
|
41
|
-
def calculate_cents(number)
|
|
41
|
+
def calculate_cents(number, with_cents)
|
|
42
42
|
# remove anything that's not a number, potential delimiter, or minus sign
|
|
43
43
|
num = number.gsub(/[^\d|\.|,|\'|\s|\-]/, '').strip
|
|
44
44
|
|
|
@@ -127,17 +127,21 @@ class String
|
|
|
127
127
|
|
|
128
128
|
# build the string based on major/minor since separator/delimiters have been removed
|
|
129
129
|
# avoiding floating point arithmetic here to ensure accuracy
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
cents +=
|
|
130
|
+
if with_cents and minor == 0
|
|
131
|
+
cents = major.to_i
|
|
132
|
+
else
|
|
133
|
+
cents = major.to_i * 100
|
|
134
|
+
# add the minor number as well. this may have any number of digits,
|
|
135
|
+
# so we treat minor as a string and truncate or right-fill it with zeroes
|
|
136
|
+
# until it becomes a two-digit number string, which we add to cents.
|
|
137
|
+
minor = minor.to_s
|
|
138
|
+
truncated_minor = minor[0..1]
|
|
139
|
+
truncated_minor << "0" * (2 - truncated_minor.size) if truncated_minor.size < 2
|
|
140
|
+
cents += truncated_minor.to_i
|
|
141
|
+
# respect rounding rules
|
|
142
|
+
if minor.size >= 3 && minor[2..2].to_i >= 5
|
|
143
|
+
cents += 1
|
|
144
|
+
end
|
|
141
145
|
end
|
|
142
146
|
|
|
143
147
|
# if negative, multiply by -1; otherwise, return positive cents
|
data/money.gemspec
CHANGED