2014-12-05 109 views
1

如何获得浮点数的单词对等?例如。将浮点数转换为单词

  • 10.24→十点24
  • 5.113→五点一三
+0

你的意思是,你有一个号码,你想获得那个数字的词表示?确切地说,是 – daremkd 2014-12-06 00:41:23

+0

!我想获得浮点数字 – omrfarukcan 2014-12-06 01:09:24

+0

我建议你使用语言学宝石,看我的答案。 – daremkd 2014-12-06 01:15:51

回答

4

有一个名为numbers_and_words为宝石!到目前为止,我已经在几个项目中使用它,没有任何问题。

2

使用linguistics宝石:

require 'linguistics' 
Linguistics.use(:en) 

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

或尝试使用这个技巧在this answer描述得到更精确:

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"