gpt4 book ai didi

ruby - 数字到英文单词 ruby

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:30 25 4
gpt4 key购买 nike

我正在尝试编写一个方法,它将接受一个数字并返回它的字符串值。我能够返回哈希中的所有内容。但是,我无法返回 43 之类的东西。我最初考虑首先检查参数是否有超过 1 个整数……然后拆分它们。我不知道。任何人都知道我如何写出 55、78、91 之类的数字?

def into_word(int)

hash = {0 => "zero",
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen",
20 => "twenty",
30 => "thirty",
40 => "forty",
50 => "fifty",
60 => "sixty",
70 => "seventy",
80 => "eighty",
90 => "ninety",
100 => "one hundred"
}

if hash.has_key?(int)
return hash[int]
# elsif int.to_s.length == 2

end

end

puts into_word(1)
puts into_word(2)
puts into_word(0)
puts into_word(4)
puts into_word(7)
puts into_word(9)
puts into_word(10)
puts into_word(42)

我正在尝试不使用任何类型的 gem 来做到这一点。

最佳答案

Rosetta Code有一个算法可以做到这一点,它工作得非常好,也可以处理大得离谱的数字。

关于ruby - 数字到英文单词 ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com