gpt4 book ai didi

python - 将字符串转换为 int,反之亦然

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:11 25 4
gpt4 key购买 nike

我需要在进行一些处理后将字符串转换为 int 并返回。字符串必须像下面这样:

45 - 四十五
99 - 九十九

我到处搜索提示,但找不到任何提示。我知道明显的 switch-case 逻辑,但我很好奇是否有更智能的方法来使用更少的代码行。

最佳答案

使用 num2words,参见( https://www.geeksforgeeks.org/python-number-to-words-using-num2words/ )

  from num2words import num2words
def num2word(num):
"""
convert numbers in a string to their word representation
"""
try:
return num2words(num, lang='en_IN')
except:
return num

关于python - 将字符串转换为 int,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7041055/

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