class NumbersAndWords::Wrappers::Float

Constants

ZERO_SYMBOL

Attributes

number[RW]
options[RW]

Public Class Methods

new(number) click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 10
def initialize(number)
  @number = number
end

Public Instance Methods

to_words(options = {}) click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 14
def to_words(options = {})
  @options = options
  words = []
  words << integral_part_with(options)
  words << fractional_part_with(options) unless fractional_part_is_nil?
  NumbersAndWords::WordsArray.new(words).join options
end

Private Instance Methods

fractional_options() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 52
def fractional_options
  length = precision || fractional_part.length
  { fractional: { length: } }
end
fractional_part() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 34
def fractional_part
  part = parts.last
  part += ZERO_SYMBOL * (precision - part.length) if precision
  part
end
fractional_part_is_nil?() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 57
def fractional_part_is_nil?
  fractional_part.to_i.zero?
end
fractional_part_with(options) click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 44
def fractional_part_with(options)
  fractional_part.to_i.to_words options.merge(fractional_options)
end
integral_options() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 48
def integral_options
  { integral: {} }
end
integral_part() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 30
def integral_part
  parts.first
end
integral_part_with(options) click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 40
def integral_part_with(options)
  integral_part.to_i.to_words options.merge(integral_options)
end
parts() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 26
def parts
  number.to_s.split '.'
end
precision() click to toggle source
# File lib/numbers_and_words/wrappers/float.rb, line 61
def precision
  options.fetch(:precision, nil)
end