def digital_to_chinese(digital): str_digital = str(digital) chinese = {'1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖', '0': '零'} chinese2 = ['拾', '佰', '仟', '万', '厘', '分', '角'] jiao = '' bs = str_digital.split('.') yuan = bs[0] if len(bs) > 1: jiao = bs[1] r_yuan = [i for i in reversed(yuan)] count = 0 for i in range(len(yuan)): if i == 0: r_yuan[i] += '圆' continue r_yuan[i] += chinese2[count] count += 1 if count == 4: count = 0 chinese2[3] = '亿' s_jiao = [i for i in jiao][:3] # 去掉小于厘之后的 j_count = -1 for i in range(len(s_jiao)): s_jiao[i] += chinese2[j_count] j_count -= 1 last = [i for i in reversed(r_yuan)] + s_jiao last_str = ''.join(last) print(f'{float(str_digital):,.3f}') print(last_str) last_str = last_str.replace('0百', '0').replace('0十', '0').replace('000', '0').replace('00', '0').replace('0圆', '圆') for i in range(len(last_str)): digital = last_str[i] if digital in chinese: last_str = last_str.replace(digital, chinese[digital]) print(last_str) return last_str if __name__ == '__main__': digital_to_chinese(123000789.456)
标签:count,中文,last,数字,jiao,大写,str,digital,yuan From: https://www.cnblogs.com/daizichuan/p/17488945.html