formatted-money 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8108feda721b9b4a1ed9df8ddd95b1fc62dd27d
4
+ data.tar.gz: 7834eab0828b82a007d0edc9b04f715e2212c6e8
5
+ SHA512:
6
+ metadata.gz: f350d6eef1d16d47bc418e12d614ca0c4776e33c724ec1d7a62f074cba4eb9c1762a22290d9a8bb834c4b06de5847e7c77202f0c0608d1cde5662bf5569a0dc9
7
+ data.tar.gz: d0a1736d403a27467daa373ddd525e6f1b65342b6f3ac655baac4aaf962ee4ea86af8dec59dcc5b136dfeb3641ce0de992a327e4b0f001a9dc8906066e2fc0b2
data/README.md CHANGED
@@ -5,30 +5,32 @@ For all Rubyist that use Integer for storing money values as cents. This is a de
5
5
  ## Installation and Requirements
6
6
 
7
7
  Install the gem and require it:
8
- ```ruby
8
+ ```
9
9
  $ gem install formatted-money
10
-
10
+ ```
11
+ ```ruby
11
12
  require 'formatted-money'
12
13
  ```
13
- formatted-money requires Ruby 1.9.2. Don't use it with 1.8.x, 1.9.x should work though.
14
+ formatted-money requires Ruby 1.9.3. Don't use it with 1.8.x, 1.9.x should work though.
15
+
14
16
  ## Usage
15
17
 
16
18
  Formatted float to cents:
17
19
  ```ruby
18
- FormattedMoney.cents('1.394.000,56') => 139400056
20
+ FormattedMoney.cents('1.394.000,56') => 139400056
19
21
  FormattedMoney.cents('7.899.994.000,56') => 789999400056
20
22
  ```
21
23
  Cents to formatted float:
22
24
  ```ruby
23
- FormattedMoney.amount(Integer(13820000)) => "138.200,00"
25
+ FormattedMoney.amount(Integer(13820000)) => "138.200,00"
24
26
  ```
25
27
  European and American submodules are available:
26
28
  ```ruby
27
- FormattedMoney::European.cents('4.000,56') => 400056
29
+ FormattedMoney::European.cents('4.000,56') => 400056
28
30
  ```
29
- To change the defauls behaviour:
31
+ To change the defaul behaviour:
30
32
  ```ruby
31
33
  FormattedMoney.delimiter = ' '
32
34
  FormattedMoney.cents_separator = FormattedMoney::American::CENTS_SEPARATOR
33
- FormattedMoney.amount(Integer(13820000)) => "138 200,00"
34
- ```
35
+ FormattedMoney.amount(Integer(13820000)) => "138 200,00"
36
+ ```
data/Rakefile CHANGED
@@ -3,30 +3,31 @@ require 'rake'
3
3
  require 'rake/testtask'
4
4
 
5
5
  gemspec = Gem::Specification.new do |s|
6
- s.name = "formatted-money"
7
- s.version = '0.0.1'
8
- s.author = "Josef Strzibny"
9
- s.email = "strzibny@strzibny.name"
10
- s.homepage = "http://github.com/strzibny/formatted-money"
6
+ s.name = 'formatted-money'
7
+ s.version = '0.0.2'
8
+ s.author = 'Josef Strzibny'
9
+ s.email = 'strzibny@strzibny.name'
10
+ s.homepage = 'http://github.com/strzibny/formatted-money'
11
11
  s.platform = Gem::Platform::RUBY
12
- s.summary = "A dead simple library for converting human-readable money inputs to Integer and back"
12
+ s.summary = 'A dead simple library for converting human-readable money inputs to Integer and back'
13
13
  s.description = <<-EOF
14
14
  For all Rubyist that use Integer for storing money values as cents.
15
15
  This is a dead simple gem for converting money from user inputs to Integer values
16
16
  for storing and fast precise calculations (and back). Does everything you need
17
17
  and nothing else. Well tested.
18
18
  EOF
19
- s.require_path = "lib"
20
- s.required_ruby_version = '>= 1.9.0'
21
- s.files = FileList["LICENSE", "Rakefile", "README.md", "doc/**/*", "lib/**/*", "test/*"]
19
+ s.require_path = 'lib'
20
+ s.required_ruby_version = '>= 1.9.3'
21
+ s.files = FileList['LICENSE', 'Rakefile', 'README.md', 'doc/**/*', 'lib/**/*', 'test/*']
22
22
  end
23
23
 
24
- Gem::PackageTask.new gemspec do |p|
25
-
24
+ Gem::PackageTask.new gemspec do |_p|
26
25
  end
27
-
26
+
28
27
  Rake::TestTask.new('test') do |t|
29
28
  t.libs << 'test'
30
29
  t.pattern = 'test/test_*.rb'
31
30
  t.verbose = true
32
- end
31
+ end
32
+
33
+ task :default => [:test]
@@ -4,163 +4,169 @@
4
4
  # @license MIT
5
5
  # @author Josef Strzibny
6
6
  module FormattedMoney
7
- class NumberNotInFloatFormat < StandardError; end;
8
- class NumberNotInIntegerFormat < StandardError; end;
7
+ class NumberNotInFloatFormat < StandardError; end
8
+ class NumberNotInIntegerFormat < StandardError; end
9
9
 
10
10
  # American defaults
11
11
  # comma for delimiter and dot for cents separator
12
12
  module American
13
13
  DELIMITER = ','
14
14
  CENTS_SEPARATOR = '.'
15
-
16
- def self.cents(amount, round = true)
15
+
16
+ def self.cents(amount, round = true)
17
17
  FormattedMoney.cents(amount, round, DELIMITER, CENTS_SEPARATOR)
18
- end
19
-
20
- def self.amount(amount, omit_cents = false)
21
- FormattedMoney.amount(amount, omit_cents, DELIMITER, CENTS_SEPARATOR)
22
- end
18
+ end
19
+
20
+ def self.amount(amount, omit_cents = false)
21
+ FormattedMoney.amount(amount, omit_cents, DELIMITER, CENTS_SEPARATOR)
22
+ end
23
23
  end
24
-
24
+
25
25
  # European defaults
26
26
  # dot for delimiter and comma for cents separator
27
27
  module European
28
28
  DELIMITER = '.'
29
29
  CENTS_SEPARATOR = ','
30
-
30
+
31
31
  def self.cents(amount, round = true)
32
- FormattedMoney.cents(amount, round, DELIMITER, CENTS_SEPARATOR)
32
+ FormattedMoney.cents(amount, round, DELIMITER, CENTS_SEPARATOR)
33
33
  end
34
34
 
35
35
  def self.amount(amount, omit_cents = false)
36
36
  FormattedMoney.amount(amount, omit_cents, DELIMITER, CENTS_SEPARATOR)
37
37
  end
38
38
  end
39
-
39
+
40
40
  @@omit_cents = false
41
41
  @@round = true
42
42
  @@delimiter = FormattedMoney::European::DELIMITER
43
43
  @@cents_separator = FormattedMoney::European::CENTS_SEPARATOR
44
44
  @@escape_chars = ['.', '^', '$', '*']
45
-
45
+
46
46
  def self.escape_chars=(chars)
47
- @@escape_chars = chars
48
- end
49
-
47
+ @@escape_chars = chars
48
+ end
49
+
50
50
  def self.omit_cents=(bool)
51
- @@omit_cents = bool
52
- end
53
-
54
- def self.round=(bool)
55
- @@round = bool
56
- end
57
-
58
- def self.delimiter=(delimiter)
59
- @@delimiter = delimiter
60
- end
61
-
62
- def self.cents_separator=(cents_separator)
63
- @@cents_separator = cents_separator
64
- end
65
-
66
- # Format Integer to float number with cents
67
- # @return [String] amount in human-readable format
68
- def self.amount(amount, omit_cents = @@omit_cents, delimiter = @@delimiter, cents_separator = @@cents_separator)
69
- unless amount.is_a? Integer; self.check_integer(amount); end
70
-
71
- amount = amount.to_s
72
- cents = amount[-2,2]
73
- cents ||= '00'
74
- units = amount[0..-3]
75
-
76
- if units.empty?
77
- if omit_cents; return '0'; end
78
-
79
- return String('0' + cents_separator + cents)
80
- end
81
-
82
- if omit_cents; return String(self.number_with_delimiter(units, delimiter)); end
83
-
84
- String(self.number_with_delimiter(units, delimiter) + cents_separator + cents)
85
- end
86
-
87
- # Format the float-formatted input to Integer for saving or for calculation
88
- # @return [Integer] money as cents
89
- def self.cents(amount, round = @@round, delimiter = @@delimiter, cents_separator = @@cents_separator)
90
- delimiter_regex = self.separator_regex(delimiter)
91
- cents_separator_regex = self.separator_regex(cents_separator)
92
-
93
- # Only zeros
94
- if self.only_zeros?(amount, delimiter_regex, cents_separator_regex); return 0; end;
95
-
96
- # Already integer
97
- if amount.is_a? Integer; return self.add_zero_cents amount; end;
98
-
99
- self.check_float(amount)
100
-
51
+ @@omit_cents = bool
52
+ end
53
+
54
+ def self.round=(bool)
55
+ @@round = bool
56
+ end
57
+
58
+ def self.delimiter=(delimiter)
59
+ @@delimiter = delimiter
60
+ end
61
+
62
+ def self.cents_separator=(cents_separator)
63
+ @@cents_separator = cents_separator
64
+ end
65
+
66
+ # Format Integer to float number with cents
67
+ # @return [String] amount in human-readable format
68
+ def self.amount(amount, omit_cents = @@omit_cents, delimiter = @@delimiter, cents_separator = @@cents_separator)
69
+ check_integer(amount) unless amount.is_a? Integer
70
+
71
+ amount = amount.to_s
72
+ cents = amount[-2, 2]
73
+ cents ||= '00'
74
+ units = amount[0..-3]
75
+
76
+ if units.empty?
77
+ return '0' if omit_cents
78
+
79
+ return String('0' + cents_separator + cents)
80
+ end
81
+
82
+ return String(number_with_delimiter(units, delimiter)) if omit_cents
83
+
84
+ String(number_with_delimiter(units, delimiter) + cents_separator + cents)
85
+ end
86
+
87
+ # Format the float-formatted input to Integer for saving or for calculation
88
+ # @return [Integer] money as cents
89
+ def self.cents(amount, round = @@round, delimiter = @@delimiter, cents_separator = @@cents_separator)
90
+ delimiter_regex = self.separator_regex(delimiter)
91
+ cents_separator_regex = self.separator_regex(cents_separator)
92
+
93
+ # Only zeros
94
+ return 0 if self.only_zeros?(amount, delimiter_regex, cents_separator_regex)
95
+
96
+ # Already integer
97
+ return add_zero_cents amount if amount.is_a? Integer
98
+
99
+ check_float(amount)
100
+ if amount[0] == '-'
101
+ amount[0] = ''
102
+ negative = true
103
+ else
104
+ negative = false
105
+ end
106
+
101
107
  # No cents
102
108
  amount = amount.delete(delimiter)
103
- if /^\d+$/.match amount; return self.add_zero_cents amount; end;
104
-
105
- # With cents
106
- units = amount.gsub(/#{cents_separator_regex}\d+/,'')
107
- cents = amount.gsub(/#{units}#{cents_separator_regex}/,'')
108
-
109
- # Round the last cent
110
- unless cents[2].nil? and round
111
- last_cent = cents[1]
112
-
113
- if Integer(cents[2]) >= 5; last_cent = Integer(last_cent) + 1; end
114
-
115
- return Integer(units + cents[0] + String(last_cent))
116
- end
117
-
118
- Integer(units + cents)
119
- end
120
-
121
- def self.add_zero_cents(n)
122
- Integer(sprintf("%d00", n))
123
- end
124
-
125
- # Check if the given number is formatted as a float -
126
- # numbers should contain only digits, commas or dots.
127
- # Only numbers like 1.000.000.000,2056 or 2,150.3 are accepted
128
- def self.check_float(n)
129
- unless /^[\d\.\,]*$/.match(n.to_s)
130
- throw NumberNotInFloatFormat, 'Float numbers can only be made out of digits, commas (,) or dots (.).'
131
- end
132
- end
133
-
134
- def self.check_integer(n)
135
- unless /^\d*$/.match(n.to_s)
136
- throw NumberNotInIntegerFormat, 'Integer numbers can only be made out of digits.'
137
- end
138
- end
139
-
140
- def self.number_with_delimiter(number, delimiter)
109
+ return add_zero_cents amount if /^\d+$/.match amount
110
+
111
+ # With cents
112
+ units = amount.gsub(/#{cents_separator_regex}\d+/, '')
113
+ cents = amount.gsub(/#{units}#{cents_separator_regex}/, '')
114
+
115
+ # Round the last cent
116
+ unless cents[2].nil? && round
117
+ last_cent = cents[1]
118
+
119
+ last_cent = Integer(last_cent) + 1 if Integer(cents[2]) >= 5
120
+
121
+ return Integer(units + cents[0] + String(last_cent))
122
+ end
123
+
124
+ if negative
125
+ Integer('-' + units + cents)
126
+ else
127
+ Integer(units + cents)
128
+ end
129
+ end
130
+
131
+ def self.add_zero_cents(n)
132
+ Integer(sprintf('%d00', n))
133
+ end
134
+
135
+ # Check if the given number is formatted as a float -
136
+ # numbers should contain only digits, commas or dots.
137
+ # Only numbers like 1.000.000.000,2056 or 2,150.3 are accepted
138
+ def self.check_float(n)
139
+ unless /^(-){0,1}[\d\.\,]*$/.match(n.to_s)
140
+ throw NumberNotInFloatFormat, 'Float numbers can only be made out of digits, commas (,) or dots (.). Dash (-) is accepted at the beginning for negative numbers'
141
+ end
142
+ end
143
+
144
+ def self.check_integer(n)
145
+ unless /^(-){0,1}\d*$/.match(n.to_s)
146
+ throw NumberNotInIntegerFormat, 'Integer numbers can only be made out of digits. Dash (-) is accepted at the beginning for negative numbers'
147
+ end
148
+ end
149
+
150
+ def self.number_with_delimiter(number, delimiter)
141
151
  number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
142
152
  end
143
-
153
+
144
154
  def self.trim_leading_zeros(number)
145
155
  string = number.to_s.gsub(/^[0]*(.*)/, '\\1')
146
156
  string = '0' if string.empty?
147
-
157
+
148
158
  string
149
159
  end
150
-
160
+
151
161
  def self.separator_regex(separator)
152
- if @@escape_chars.include? separator
153
- return "\\" + "#{separator}"
154
- end
155
-
156
- return "#{separator}"
157
- end
158
-
159
- def self.only_zeros?(number, delimiter, cents_separator)
160
- unless /^[0#{delimiter}#{cents_separator}]*$/.match(number)
161
- return false;
162
- end
163
-
164
- return true;
162
+ return '\\' + "#{separator}" if @@escape_chars.include? separator
163
+
164
+ "#{separator}"
165
+ end
166
+
167
+ def self.only_zeros?(number, delimiter, cents_separator)
168
+ return false unless /^[0#{delimiter}#{cents_separator}]*$/.match(number)
169
+
170
+ true
165
171
  end
166
- end
172
+ end
@@ -1,138 +1,150 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'test-unit'
4
- require 'formatted_money'
3
+ require 'minitest/autorun'
4
+ require 'formatted-money'
5
5
 
6
- class TestFormattedMoney < Test::Unit::TestCase
7
-
6
+ class TestFormattedMoney < Minitest::Test
7
+ # Defaults to European style
8
8
  def test_conversion_to_cents
9
- # Defaults to European style
10
9
  assert_equal Integer(0), FormattedMoney.cents('0')
11
10
  assert_equal Integer(0), FormattedMoney.cents('0,00')
12
- assert_equal Integer(400056), FormattedMoney.cents('4.000,56')
13
- assert_equal Integer(4200056), FormattedMoney.cents('42.000,56')
14
- assert_equal Integer(139400056), FormattedMoney.cents('1.394.000,56')
15
- assert_equal Integer(789999400056), FormattedMoney.cents('7.899.994.000,56')
11
+ assert_equal Integer(400_056), FormattedMoney.cents('4.000,56')
12
+ assert_equal Integer(4_200_056), FormattedMoney.cents('42.000,56')
13
+ assert_equal Integer(139_400_056), FormattedMoney.cents('1.394.000,56')
14
+ assert_equal Integer(789_999_400_056), FormattedMoney.cents('7.899.994.000,56')
16
15
  end
17
-
16
+
18
17
  def test_european_conversion_to_cents
19
18
  assert_equal Integer(0), FormattedMoney::European.cents('0')
20
19
  assert_equal Integer(0), FormattedMoney::European.cents('0,00')
21
- assert_equal Integer(400056), FormattedMoney::European.cents('4.000,56')
22
- assert_equal Integer(4200056), FormattedMoney::European.cents('42.000,56')
23
- assert_equal Integer(139400056), FormattedMoney::European.cents('1.394.000,56')
24
- assert_equal Integer(789999400056), FormattedMoney::European.cents('7.899.994.000,56')
20
+ assert_equal Integer(400_056), FormattedMoney::European.cents('4.000,56')
21
+ assert_equal Integer(4_200_056), FormattedMoney::European.cents('42.000,56')
22
+ assert_equal Integer(139_400_056), FormattedMoney::European.cents('1.394.000,56')
23
+ assert_equal Integer(789_999_400_056), FormattedMoney::European.cents('7.899.994.000,56')
25
24
  end
26
-
25
+
27
26
  def test_american_conversion_to_cents
28
27
  assert_equal Integer(0), FormattedMoney::American.cents('0')
29
28
  assert_equal Integer(0), FormattedMoney::American.cents('0.00')
30
- assert_equal Integer(400056), FormattedMoney::American.cents('4,000.56')
31
- assert_equal Integer(4200056), FormattedMoney::American.cents('42,000.56')
32
- assert_equal Integer(139400056), FormattedMoney::American.cents('1,394,000.56')
33
- assert_equal Integer(789999400056), FormattedMoney::American.cents('7,899,994,000.56')
29
+ assert_equal Integer(400_056), FormattedMoney::American.cents('4,000.56')
30
+ assert_equal Integer(4_200_056), FormattedMoney::American.cents('42,000.56')
31
+ assert_equal Integer(139_400_056), FormattedMoney::American.cents('1,394,000.56')
32
+ assert_equal Integer(789_999_400_056), FormattedMoney::American.cents('7,899,994,000.56')
34
33
  end
35
-
34
+
35
+ # Defaults to European style
36
36
  def test_formatted_amount
37
- # Defaults to European style
38
37
  assert_equal '0,00', FormattedMoney.amount(Integer(0))
39
38
  assert_equal '0,00', FormattedMoney.amount(Integer(00))
40
- assert_equal '1.000,00', FormattedMoney.amount(Integer(100000))
41
- assert_equal '1.234,56', FormattedMoney.amount(Integer(123456))
42
- assert_equal '138.200,00', FormattedMoney.amount(Integer(13820000))
43
- assert_equal '138.555.200,00', FormattedMoney.amount(Integer(13855520000))
44
- assert_equal '26.897.200,00', FormattedMoney.amount(Integer(2689720000))
39
+ assert_equal '1.000,00', FormattedMoney.amount(Integer(100_000))
40
+ assert_equal '1.234,56', FormattedMoney.amount(Integer(123_456))
41
+ assert_equal '138.200,00', FormattedMoney.amount(Integer(13_820_000))
42
+ assert_equal '138.555.200,00', FormattedMoney.amount(Integer(13_855_520_000))
43
+ assert_equal '26.897.200,00', FormattedMoney.amount(Integer(2_689_720_000))
45
44
  end
46
-
45
+
46
+ # Strings containing only numbers should be fine as well
47
47
  def test_formatted_amount_from_string
48
- # Strings containing only numbers should be fine as well
49
48
  assert_equal '0,00', FormattedMoney.amount(String('0'))
50
49
  assert_equal '0,00', FormattedMoney.amount(String('00'))
51
50
  assert_equal '138.555.200,00', FormattedMoney.amount('13855520000')
52
51
  end
53
-
52
+
54
53
  def test_europen_formatted_amount
55
54
  assert_equal '0,00', FormattedMoney::European.amount(Integer(0))
56
55
  assert_equal '0,00', FormattedMoney::European.amount(Integer(00))
57
- assert_equal '1.000,00', FormattedMoney::European.amount(Integer(100000))
58
- assert_equal '1.234,56', FormattedMoney::European.amount(Integer(123456))
59
- assert_equal '138.200,00', FormattedMoney::European.amount(Integer(13820000))
60
- assert_equal '138.555.200,00', FormattedMoney::European.amount(Integer(13855520000))
61
- assert_equal '26.897.200,00', FormattedMoney::European.amount(Integer(2689720000))
56
+ assert_equal '1.000,00', FormattedMoney::European.amount(Integer(100_000))
57
+ assert_equal '1.234,56', FormattedMoney::European.amount(Integer(123_456))
58
+ assert_equal '138.200,00', FormattedMoney::European.amount(Integer(13_820_000))
59
+ assert_equal '138.555.200,00', FormattedMoney::European.amount(Integer(13_855_520_000))
60
+ assert_equal '26.897.200,00', FormattedMoney::European.amount(Integer(2_689_720_000))
62
61
  end
63
-
62
+
64
63
  def test_american_formatted_amount
65
64
  assert_equal '0.00', FormattedMoney::American.amount(Integer(0))
66
65
  assert_equal '0.00', FormattedMoney::American.amount(Integer(00))
67
- assert_equal '1,000.00', FormattedMoney::American.amount(Integer(100000))
68
- assert_equal '1,234.56', FormattedMoney::American.amount(Integer(123456))
69
- assert_equal '138,200.00', FormattedMoney::American.amount(Integer(13820000))
70
- assert_equal '138,555,200.00', FormattedMoney::American.amount(Integer(13855520000))
71
- assert_equal '26,897,200.00', FormattedMoney::American.amount(Integer(2689720000))
66
+ assert_equal '1,000.00', FormattedMoney::American.amount(Integer(100_000))
67
+ assert_equal '1,234.56', FormattedMoney::American.amount(Integer(123_456))
68
+ assert_equal '138,200.00', FormattedMoney::American.amount(Integer(13_820_000))
69
+ assert_equal '138,555,200.00', FormattedMoney::American.amount(Integer(13_855_520_000))
70
+ assert_equal '26,897,200.00', FormattedMoney::American.amount(Integer(2_689_720_000))
72
71
  end
73
-
72
+
74
73
  def test_check_float
75
74
  FormattedMoney.check_float('123,254')
76
75
  FormattedMoney.check_float('3,254.456')
77
-
78
- assert_raises (ArgumentError) {
76
+
77
+ assert_raises (ArgumentError) do
79
78
  FormattedMoney.check_float('123254d')
80
- }
81
- end
82
-
83
- def test_check_integer
84
- FormattedMoney.check_integer('123254')
85
- FormattedMoney.check_integer(123254)
86
-
87
- assert_raises (ArgumentError) {
88
- FormattedMoney.check_integer('123254d')
89
- }
90
- end
91
-
79
+ end
80
+ end
81
+
82
+ def test_check_integer
83
+ FormattedMoney.check_integer('123254')
84
+ FormattedMoney.check_integer(123_254)
85
+
86
+ assert_raises (ArgumentError) do
87
+ FormattedMoney.check_integer('123254d')
88
+ end
89
+ end
90
+
92
91
  def test_number_with_delimiter
93
- assert_equal '1.000.000', FormattedMoney.number_with_delimiter('1000000','.')
94
- assert_equal '400|000', FormattedMoney.number_with_delimiter('400000','|')
95
- assert_equal '21,000,000,156', FormattedMoney.number_with_delimiter('21000000156',',')
96
-
97
- assert_equal '1 000 000', FormattedMoney.number_with_delimiter('1000000',' ')
98
- assert_equal '400 000', FormattedMoney.number_with_delimiter('400000',' ')
99
- assert_equal '21 000 000 156', FormattedMoney.number_with_delimiter('21000000156',' ')
100
-
101
- assert_equal '1.000.000', FormattedMoney.number_with_delimiter(1000000,'.')
102
- assert_equal '400|000', FormattedMoney.number_with_delimiter(400000,'|')
103
- assert_equal '21,000,000,156', FormattedMoney.number_with_delimiter(21000000156,',')
92
+ assert_equal '1.000.000', FormattedMoney.number_with_delimiter('1000000', '.')
93
+ assert_equal '400|000', FormattedMoney.number_with_delimiter('400000', '|')
94
+ assert_equal '21,000,000,156', FormattedMoney.number_with_delimiter('21000000156', ',')
95
+
96
+ assert_equal '1 000 000', FormattedMoney.number_with_delimiter('1000000', ' ')
97
+ assert_equal '400 000', FormattedMoney.number_with_delimiter('400000', ' ')
98
+ assert_equal '21 000 000 156', FormattedMoney.number_with_delimiter('21000000156', ' ')
99
+
100
+ assert_equal '1.000.000', FormattedMoney.number_with_delimiter(1_000_000, '.')
101
+ assert_equal '400|000', FormattedMoney.number_with_delimiter(400_000, '|')
102
+ assert_equal '21,000,000,156', FormattedMoney.number_with_delimiter(21_000_000_156, ',')
104
103
  end
105
-
104
+
106
105
  def test_trim_leading_zeros
107
106
  assert_equal '0', FormattedMoney.trim_leading_zeros('0')
108
107
  assert_equal '525', FormattedMoney.trim_leading_zeros('0525')
109
108
  assert_equal '2', FormattedMoney.trim_leading_zeros('0002')
110
109
  assert_equal '300', FormattedMoney.trim_leading_zeros('0300')
111
110
  end
112
-
111
+
113
112
  def test_separator_regex
114
113
  FormattedMoney.escape_chars = ['.', '^', '$', '*']
115
-
114
+
116
115
  assert_equal '\\.', FormattedMoney.separator_regex('.')
117
116
  assert_equal ',', FormattedMoney.separator_regex(',')
118
-
117
+
119
118
  FormattedMoney.escape_chars = []
120
-
119
+
121
120
  assert_equal '.', FormattedMoney.separator_regex('.')
121
+
122
+ # Back to default
123
+ FormattedMoney.escape_chars = ['.', '^', '$', '*']
122
124
  end
123
-
125
+
124
126
  def test_only_zeros
125
127
  delimiter = '\.'
126
128
  cents_separator = ','
127
-
128
- assert_equal true, FormattedMoney.only_zeros?('0', delimiter, cents_separator)
129
- assert_equal true, FormattedMoney.only_zeros?('0,0', delimiter, cents_separator)
130
- assert_equal true, FormattedMoney.only_zeros?('0,00', delimiter, cents_separator)
131
- assert_equal true, FormattedMoney.only_zeros?('0.00', delimiter, cents_separator)
132
- assert_equal true, FormattedMoney.only_zeros?('0.000,0', delimiter, cents_separator)
133
- assert_equal true, FormattedMoney.only_zeros?(',0', delimiter, cents_separator)
134
- assert_equal false, FormattedMoney.only_zeros?('01', delimiter, cents_separator)
135
- assert_equal false, FormattedMoney.only_zeros?('0|0', delimiter, cents_separator)
136
- assert_equal false, FormattedMoney.only_zeros?('000500', delimiter, cents_separator)
129
+
130
+ assert_equal true, FormattedMoney.only_zeros?('0', delimiter, cents_separator)
131
+ assert_equal true, FormattedMoney.only_zeros?('0,0', delimiter, cents_separator)
132
+ assert_equal true, FormattedMoney.only_zeros?('0,00', delimiter, cents_separator)
133
+ assert_equal true, FormattedMoney.only_zeros?('0.00', delimiter, cents_separator)
134
+ assert_equal true, FormattedMoney.only_zeros?('0.000,0', delimiter, cents_separator)
135
+ assert_equal true, FormattedMoney.only_zeros?(',0', delimiter, cents_separator)
136
+ assert_equal false, FormattedMoney.only_zeros?('01', delimiter, cents_separator)
137
+ assert_equal false, FormattedMoney.only_zeros?('0|0', delimiter, cents_separator)
138
+ assert_equal false, FormattedMoney.only_zeros?('000500', delimiter, cents_separator)
139
+ end
140
+
141
+ def test_negative_numbers
142
+ assert_equal '-1.000,00', FormattedMoney::European.amount(Integer(-100_000))
143
+ assert_equal '-1.234,56', FormattedMoney::European.amount(Integer(-123_456))
144
+ assert_equal '-138.200,00', FormattedMoney::European.amount(Integer(-13_820_000))
145
+ assert_equal '-138.555.200,00', FormattedMoney::European.amount(Integer(-13_855_520_000))
146
+ assert_equal '-26.897.200,00', FormattedMoney::European.amount(Integer(-2_689_720_000))
147
+ assert_equal Integer(-139_400_056), FormattedMoney::American.cents('-1,394,000.56')
148
+ assert_equal Integer(-789_999_400_056), FormattedMoney::American.cents('-7,899,994,000.56')
137
149
  end
138
- end
150
+ end