Convert floating-point number to words

966 views Asked by At

How do I get the word-equivalent of a floating-point number? E.g.

  • 10.24 → ten point twenty-four
  • 5.113 → five point hundred and thirteen
2

There are 2 answers

0
Alex On

There's a gem called numbers_and_words for that! I've used it in several projects without any problems so far.

0
daremkd On

Use the linguistics gem:

require 'linguistics'
Linguistics.use( :en )

p 10.24.en.numwords #=> "ten point two four"
p 5.113.en.numwords #=> "five point one one three"

or try to use this hack as described in this answer to get more precision:

require "linguistics"
Linguistics::use(:en)

class Float
  def my_numwords
    self.to_s.split('.').collect { |n| n.en.numwords }.join(' point ')
  end
end

p 10.24.my_numwords #=> "ten point two four"
p 5.113.my_numwords #=> ""five point one hundred and thirteen"