英文单词转换轨道的数字

Pal*_*nan 26 ruby-on-rails

有人知道在rails中将数字转换为英文数字的方法吗?

我找到了一些Ruby脚本来将数字转换为英文单词以获得相应的单词.

而不是在ruby中编写脚本,我觉得可以使用直接功能.

例如.1 - >一,二 - >二.

Geo*_*roy 11

还有人性化的宝石完全符合您的要求......

require 'humanize'
23.humanize # => "twenty three"
0.42.humanize(decimals_as: :digits) # => "zero point four two"
Run Code Online (Sandbox Code Playgroud)

  • 我得到'undefined方法'人性化'为1:Fixnum`,或`1.to_s.humanize#> 1 (3认同)
  • @ p.matsinopoulos`1.humanize`,但你首先需要'要求'人性化' (2认同)
  • @alexanderbird点击链接.这是宝石! (2认同)

Mis*_*cha 10

不,你必须自己写一个函数.最接近您想要的是number_to_human,但不会转换1One.

以下是一些可能有用的网址:


小智 5

您还可以使用to_words gem.

这个宝石将整数转换成单词.

例如

1.to_words # one ,

100.to_words # one hundred ,

101.to_words # one hundred and one
Run Code Online (Sandbox Code Playgroud)

它还转换负数.