def numwords( number, hashargs={} )
num = number.to_s
config = NumwordDefaults.merge( hashargs )
raise "Bad chunking option: #{config[:group]}" unless
config[:group].between?( 0, 3 )
parts = []
sign = (/\A\s*\+/ =~ num) ? 'plus' : (/\A\s*\-/ =~ num) ? 'minus' : ''
ord = true if num.sub!( /(st|nd|rd|th)\Z/, '' )
chunks = if !config[:decimal].empty? then
if config[:group].nonzero?
num.split(/\./)
else
num.split(/\./, 2)
end
else
[ num ]
end
chunks.each_with_index {|chunk,section|
chunk.gsub!( /\D+/, '' )
if chunk.empty?
if section.zero?
parts.push []
next
end
end
unless config[:group].zero? && section.nonzero?
parts.push number_to_words( chunk, config )
else
parts.push number_to_words( chunk, config.merge(:group => 1) )
end
}
debug_msg "Parts => #{parts.inspect}"
if ord && !parts[0].empty?
parts[0][-1] = ordinal( parts[0].last )
end
if config[:asArray]
unless sign.empty?
parts[0].unshift( sign )
end
return parts.flatten
end
if config[:group].zero?
if parts[0].length > 1
wholenum = parts[0][0...-1].join( config[:comma] )
if /^\s*(\S+)\s*$/ =~ parts[0].last
wholenum += config[:and] + parts[0].last
else
wholenum += config[:comma] + parts[0].last
end
else
wholenum = parts[0][0]
end
decimals = parts[1..-1].collect {|part| part.join(" ")}
debug_msg "Wholenum: #{wholenum.inspect}; decimals: #{decimals.inspect}"
unless config[:decimal].empty?
return sign + ([ wholenum ] + decimals).
join( " #{config[:decimal]} " ).strip
else
return sign + ([ wholenum ] + decimals).
join( " " ).strip
end
else
return parts.compact.
separate( config[:decimal] ).
delete_if {|el| el.empty?}.
join( config[:comma] ).
strip
end
end