Ruby 语言学宝石

发布于 2024-08-15 12:50:32 字数 164 浏览 3 评论 0原文

我尝试将数字转换为单词,但有一个问题:

>> (91.80).en.numwords
=> "ninety-one point eight"

我希望它是“九一点八十”。我使用语言学宝石。你知道它的一些解决方案吗(更喜欢语言学)。

I try to convert number to words but I have a problem:

>> (91.80).en.numwords
=> "ninety-one point eight"

I want it to be "ninety-one point eighty". I use Linguistics gem. Do you know some solution for it (prefer with Linguistics).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-08-22 12:50:32

这有点骇人听闻,但它确实有效:

'91.80'.split('.').map {|i| i.en.numwords}.join(' point ')
=> "ninety-one point eighty"

当您将 91.80 作为浮点数输入时,Ruby 会去掉尾随的零,因此它需要以字符串开头才能保留该信息。一个更好的例子可能是:

'91.83'.split('.').map {|i| i.en.numwords}.join(' point ')
 => "ninety-one point eighty-three"

It's a bit hackish, but it works:

'91.80'.split('.').map {|i| i.en.numwords}.join(' point ')
=> "ninety-one point eighty"

When you put 91.80 as a float, ruby gets rid of the trailing zero, so it needs to be a string to begin with to retain that information. A better example might have been:

'91.83'.split('.').map {|i| i.en.numwords}.join(' point ')
 => "ninety-one point eighty-three"
沩ん囻菔务 2024-08-22 12:50:32

如果您将 Linguistics gem 与 Ruby 1.9 一起使用,则需要修补 en.rb 的第 1060 行。

# Ruby 1.8 -->  fn = NumberToWordsFunctions[ digits.nitems ]
# Ruby 1.9 removed Array.nitems so we get -->  fn = NumberToWordsFunctions[ digits.count{|x| !x.nil?} ]
fn = NumberToWordsFunctions[ digits.count{|x| !x.nil?} ]

我们已将小补丁提交给作者。

If you use the Linguistics gem with Ruby 1.9 you'll need to patch line 1060 of en.rb

# Ruby 1.8 -->  fn = NumberToWordsFunctions[ digits.nitems ]
# Ruby 1.9 removed Array.nitems so we get -->  fn = NumberToWordsFunctions[ digits.count{|x| !x.nil?} ]
fn = NumberToWordsFunctions[ digits.count{|x| !x.nil?} ]

We submitted the small patch to the author.

云归处 2024-08-22 12:50:32

我自己得到了答案。

def amount_to_words(number)
  unless (number % 1).zero?
    number = number.abs if number < 0
    div = number.div(1)                      
    mod = (number.modulo(1) * 100).round    
    [div.to_s.en.numwords, "point", mod.to_s.en.numwords].join(" ")
  else
    number.en.numwords
  end
end

结果:

>> amount_to_words(-91.83)
=> "ninety-one point eighty-three"
>> amount_to_words(-91.8)
=> "ninety-one point eighty"
>> amount_to_words(91.8)
=> "ninety-one point eighty"
>> amount_to_words(91.83)
=> "ninety-one point eighty-three"

不过,谢谢你们。你对 to_s 的想法对我很有帮助。

I got answer by myself.

def amount_to_words(number)
  unless (number % 1).zero?
    number = number.abs if number < 0
    div = number.div(1)                      
    mod = (number.modulo(1) * 100).round    
    [div.to_s.en.numwords, "point", mod.to_s.en.numwords].join(" ")
  else
    number.en.numwords
  end
end

And result:

>> amount_to_words(-91.83)
=> "ninety-one point eighty-three"
>> amount_to_words(-91.8)
=> "ninety-one point eighty"
>> amount_to_words(91.8)
=> "ninety-one point eighty"
>> amount_to_words(91.83)
=> "ninety-one point eighty-three"

Although, thank you guys. Your idea with to_s was helpful for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文