24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/polish_number.rb', line 24
def self.translate(number, options={})
if options[:currency] && !CURRENCIES.has_key?(options[:currency])
raise ArgumentError, "unknown :currency option '#{options[:currency].inspect}'. Choose one from: #{CURRENCIES.inspect}"
end
number = number.to_i
unless (0..999999).include? number
raise ArgumentError, 'number should be in 0..999999 range'
end
if number == 0
result = ZERO.dup
else
formatted_number = sprintf('%06.0f', number)
digits = formatted_number.chars.map { |char| char.to_i }
result = ''
result << process_0_999(digits[0..2])
result << thousands(number/1000, digits[0..2])
result << ' '
result << process_0_999(digits[3..5])
result.strip!
end
if options[:currency]
currency = CURRENCIES[options[:currency]]
result << ' '
result << currency[classify(number, digits)]
end
result
end
|