Module: PolishNumber
- Defined in:
- lib/polish_number.rb
Constant Summary collapse
- HUNDREDS =
['', 'sto ', 'dwieście ', 'trzysta ', 'czterysta ', 'pięćset ', 'sześćset ', 'siedemset ', 'osiemset ', 'dziewięćset ']
- TENS =
['', 'dziesięć ', 'dwadzieścia ', 'trzydzieści ', 'czterdzieści ', 'pięćdziesiąt ', 'sześćdziesiąt ', 'siedemdziesiąt ', 'osiemdziesiąt ', 'dziewięćdziesiąt ']
- TEENS =
['', 'jedenaście ', 'dwanaście ', 'trzynaście ', 'czternaście ', 'piętnaście ', 'szesnaście ', 'siedemnaście ', 'osiemnaście ', 'dziewiętnaście ']
- UNITIES =
['', 'jeden ', 'dwa ', 'trzy ', 'cztery ', 'pięć ', 'sześć ', 'siedem ', 'osiem ', 'dziewięć ']
- ZERO =
'zero'
- THOUSANDS =
{:one => 'tysiąc', :few => 'tysiące', :many => 'tysięcy'}
- CURRENCIES =
{ :PLN => {:one => 'złoty', :few => 'złote', :many => 'złotych'} }
Class Method Summary collapse
Class Method Details
.translate(number, options = {}) ⇒ Object
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, ={}) if [:currency] && !CURRENCIES.has_key?([:currency]) raise ArgumentError, "unknown :currency option '#{[: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 [:currency] currency = CURRENCIES[[:currency]] result << ' ' result << currency[classify(number, digits)] end result end |