class Abaco::Converter
Number converter
The convert converts numbers into Italian.
@author Alessandro Desantis <desa.alessandro@gmail.com>
Constants
- EXCEPTIONS
- MAX_NUMBER
- NUMBERS
- SUFFIXES
Public Class Methods
convert(number)
click to toggle source
Converts the given number into Italian words.
@param number [Numeric] the number to convert (<= 999.999.999.999)
@return [String]
A string in the 'NUMBER/DD' form, 'DD' being two decimal digits (these will be added even if the number is a Fixnum).
@raise BigNumberError
if the number is too big to be converted
# File lib/abaco/converter.rb, line 43 def self.convert(number) if number > MAX_NUMBER raise BigNumberError, "Abaco can't convert numbers bigger than #{MAX_NUMBER}" end number = number.to_f tmp = number.to_i if tmp == 0 result = 'zero' else result = '' 9.step(3, -3) do |n| # Is the number in the 10^n form? if Math.log10(tmp).to_i >= n result += describe_part(tmp / 10 ** n) result += ' ' if n >= 6 result += SUFFIXES[n] result += ' ' if n >= 6 end tmp %= 10 ** n end end decimal_part = '/' decimal_part += ('%.2f' % number).split('.').last result += describe_part(tmp) + decimal_part EXCEPTIONS.each_pair do |search, replace| result.gsub! search, replace end result end
Private Class Methods
describe_part(number)
click to toggle source
# File lib/abaco/converter.rb, line 85 def self.describe_part(number) tmp = number result = '' result += NUMBERS[:units][tmp / 100] + 'cento' if tmp >= 100 if (tmp %= 100) >= 20 result += NUMBERS[:tens][tmp / 10] elsif tmp >= 10 && tmp < 20 result += NUMBERS[:teens][tmp % 10] tmp = 0 end result + NUMBERS[:units][tmp % 10] end