Created
June 10, 2022 05:53
-
-
Save feiandxs/902b77a89e34dc4903410bb033f06d03 to your computer and use it in GitHub Desktop.
数字转汉字
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
han_list = ["零" , "一" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九"] | |
unit_list = ["","","十" , "百" , "千"] | |
def four_to_han(num_str: str): | |
result = "" | |
num_len = len(num_str) | |
for i in range(num_len): | |
num = int(num_str[i]) | |
if i!=num_len-1: | |
if num!=0: | |
result=result+han_list[num]+unit_list[num_len-i] | |
else: | |
if result[-1]=='零': | |
continue | |
else: | |
result=result+'零' | |
else: | |
if num!=0: | |
result += han_list[num] | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment