【问题标题】:Convert floating-point number to words将浮点数转换为单词
【发布时间】:2015-02-04 06:04:24
【问题描述】:

如何获得浮点数的等效字?例如

  • 10.24 → 十点二十四
  • 5.113→五点一百一十三

【问题讨论】:

  • 你的意思是,你有一个数字,你想得到那个数字的单词表示?
  • 没错!我想得到浮点数的词
  • 我推荐你使用语言学 gem,看我的回答。

标签: ruby numbers number-formatting output-formatting


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用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"
    

    或尝试使用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"
    

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2011-02-28
      • 2020-08-25
      • 2020-05-14
      相关资源
      最近更新 更多