题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import sys def process_under_hundred(val_int: int) -> str: result = "" if val_int >= 10: num_ten = val_int // 10 if num_ten == 1: result += "拾" else: result += num2ch[num_ten] + "拾" if val_int % 10 != 0: result += num2ch[val_int % 10] elif val_int > 0: result += num2ch[val_int % 10] # print("under hundred") return result def process_under_thousand(val_int) -> str: result = "" if val_int >= 100: num_hundred = val_int // 100 result += num2ch[num_hundred] + "佰" if val_int % 100 >= 10: result += process_under_hundred(val_int % 100) else: result += "零" + process_under_hundred(val_int % 100) else: result += process_under_hundred(val_int) return result def process_under_wan(val_int: int) -> str: result = "" if val_int >= 1000: num_thousand = val_int // 1000 result += num2ch[num_thousand] + "仟" if val_int % 1000 >= 100: result += process_under_thousand(val_int % 1000) else: result += "零" + process_under_thousand(val_int % 1000) else: result += process_under_thousand(val_int) return result def process_exceed_wan(val_int: int) -> str: result = "" if val_int >= 10000: num_wan = val_int // 10000 result = process_under_wan(num_wan) + "万" if val_int % 10000 < 1000: result += "零" + process_under_thousand(val_int % 10000) else: result += process_under_wan(val_int % 10000) else: result += process_under_wan(val_int) return result # 要处理变量val_int # 假如数字小于10 # 应当先处理//亿的部分,然后处理%亿,然后处理%万,千,佰,十 def process_exceed_yi(val_int: int) -> str: left_yi = val_int % 100000000 result = "" if left_yi >= 100000000: result = process_exceed_yi(left_yi) else: result = process_exceed_wan(left_yi) if val_int >= 100000000: num_yi = val_int // 100000000 if num_yi >= 100000000: result = process_exceed_yi(num_yi) + "亿" + result else: result = process_exceed_wan(num_yi) + "亿" + result return result num2ch = "零壹贰叁肆伍陆柒捌玖" bignum2ch = "拾佰仟万亿" value_str = input() val_float = float(value_str) val_int = int(val_float) str_after_dot = value_str.split(".")[1] result_prefix = "人民币" result_after_dot = "" result_before_dot = "" if str_after_dot != "00": if str_after_dot[0] != "0": result_after_dot += num2ch[int(str_after_dot[0])] + "角" if str_after_dot[1] != "0": result_after_dot += num2ch[int(str_after_dot[1])] + "分" else: result_after_dot = "整" if val_int == 0: result_int = result_prefix else: result_int = result_prefix + process_exceed_yi(val_int) + "元" print(result_int + result_after_dot)