• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python实现人民币大写转换

python 搞代码 4年前 (2022-01-08) 32次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了python实现人民币大写转换的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现人民币大写转换的具体代码,供大家参考,具体内容如下

 #!/usr/bin/python # -*- coding:utf-8 -*- # ********* 转换方法介绍 ********* # 将需要转换的数字从右向左,每4位分成一个section,如:24530467103,将该数字拆分后,得到: # 245 3046 7103 (245亿3046万7103) # 对拆分后的数字先按照section进行数字到汉字的转换,然后添加数值单位,如:仟,佰,拾,处理结束后可以得到转换后的序列。 # 对section处理结束后,再对每个section进行单位的追加。如:兆、亿、万。 # 这里需要注意一些特殊情况,如:section中连续出现0,最后一个数字为0等。 DEBUG = True upper = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"] decimal_unit = ["角", "分", "厘", "毫"] section_unit = ["万", "亿", "兆"] count_unit = ["拾", "佰", "仟"] def dbg_print(s): if DEBUG: print(s) def split_num(num): num_list = [] if (len(num) <= 4): num_list.append(num) return num_list while (len(num)): if (len(num) = 1) and ('0' != item[count+1])): result += upper[n] else (0 != n): result += upper[n] else: # section的最后一个数字非0的情况。 if (0 != n): result += u<i style="color:transparent">来源gaodai$ma#com搞$代*码*网</i>pper[n] # 最后一个数字以及数字为0时,都不需要添加单位。 if ((index >= 0) and (0 != n)): result += count_unit[index] index += 1 count += 1 从第1个section开始,如果section中的数字不全为0,其后就需要添加section对应的单位。 if (sec_index >= 0 and flag != count): result += section_unit[sec_index] dbg_print(result) sec_index -= 1 result = result.replace("壹拾", "拾") result += "元" return result # 转换函数 def convert(num): result = "" num = round(float(num), 4) integer,decimal = str(num).split('.') result_int = convert_int(integer) result_dec = convert_dec(decimal) if (len(result_dec) == 0): result = result_int += "整" else: result = result_int + result_dec return result 

以上就是python实现人民币大写转换的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python实现人民币大写转换
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址