数字转汉字
来源:互联网 发布:淘宝750海报 编辑:程序博客网 时间:2024/06/05 05:56
def num2chinese(n): chinese = "" num_list = list("零一二三四五六七八九十") pos1 = list("十百千") pos2 = list("万亿兆") n_str = str(n) if len(n_str)>16: print("数字超出范围") return -1 str_list = [] while len(n_str)>0: str_list.insert(0, n_str[-4:]) n_str = n_str[:-4] zero_flag = False flag = False for cut_num in str_list: pos2 = pos2[:(len(str_list)-1)] if cut_num=="0000": zero_flag = True if pos2: pos2.pop() continue if zero_flag: chinese += "零" if 0<int(cut_num)<=10: if cut_num[0]=="0": chinese += "零" chinese += num_list[int(cut_num)] if pos2: chinese += pos2.pop() continue pos_1 = pos1[:(len(cut_num)-1)] for num in cut_num: if num=="0": flag = True if pos_1: pos_1.pop() continue if flag: chinese += "零" chinese += num_list[int(num)] if pos_1: chinese += pos_1.pop() if pos2: chinese += pos2.pop() if chinese[0:2]=="一十": chinese = chinese[1:] return chinese
第二种实现
def num2chinese(n): danwei = list(" 十百千万十百千亿十百千兆十百千") num_chinese = list("零一二三四五六七八九") chinese="" n2str = str(n) length = len(n2str) for i, v in enumerate(n2str): chinese += num_chinese[int(v)] + danwei[length-i-1] chinese = chinese[1:] if chinese[:2]=="一十" else chinese zero_flag = False new_chinese = "" for x in chinese: if x=="零" and not zero_flag: zero_flag = True new_chinese += "零" continue if x =="零" and zero_flag: continue if zero_flag and x in num_chinese: zero_flag = False if zero_flag and x in ("十", "百", "千"): continue if zero_flag and x in ("兆","亿","万"): new_chinese = new_chinese[:-1] zero_flag = False new_chinese += x new_chinese = new_chinese.replace("亿万","亿") new_chinese = new_chinese.replace("兆亿","兆") new_chinese = new_chinese.strip().strip("零") return new_chinese
0 0
- 人民币数字转汉字
- 数字转大写汉字
- java 数字转汉字
- c++ 数字转汉字
- 数字转汉字 金钱
- 金额:数字转汉字
- 数字转汉字
- iOS数字转汉字
- 数字转汉字,含十百千
- 汉字数字转阿拉伯数字
- 数字转汉字算法
- 【js】数字转汉字中文数字
- javascript数字转汉字中文数字
- 数字转汉字大写金额
- 阿拉伯数字转汉字数字(C#)
- 数字 转 汉字【BY逍遥】
- java 数字转大写汉字
- JS数字转汉字大写
- opengl 光照计算
- 统计自然语言处理——n元语法(马尔可夫模型)小结
- Ubuntu建立和删除用户
- 【C++】【STL】STL中部分数据结构的通俗化解释
- Golang 反射
- 数字转汉字
- Firefox浏览器中,input输入框输入的内容在刷新网页后为何还在?
- 上传图片实现预览
- 优化Bitmap图片
- linux导入数据到mysq中
- 关于java.lang.IncompatibleClassChangeError: Implementing class错误解决
- 汽车零件分类编码表
- addValue:forHTTPHeaderField: http头的组成
- 测透51汇编 51单片机串口模块程序