import re
dictionary = {'0': '零', '1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖'}
level1 = {1: '圆', 2: '万', 3: '亿', 4: '兆'}
level2 = {2: '拾', 3: '佰', 4: '仟'}
level3 = {1: '角', 2: '分'}
class DecimalToChn:
def __init__(self, que_decimal):
self.que_decimal = str(que_decimal)
self.num_float = None
self.num_integer = None
self.pre = []
self.integer_chn = []
self.float_chn = []
def query(self):
'''
验证数字,整数大于16位数,小数多余小数点后面2位数的全部弄成0
:return:
'''
if "." not in self.que_decimal:
self.que_decimal = self.que_decimal + "." + "0"
if len(self.que_decimal.split('.')) <= 16 and len(self.que_decimal.split('.')) <= 2:
return self.que_decimal
else:
return 0
def split_num(self):
"""
将进来的数字拆成整数部分跟小数部分
:return:
"""
number = self.query()
self.num_integer, self.num_float = number.split('.')
def process_integer(self):
"""
处理整数部分
:return:
"""
length = len(self.num_integer)
# 整数部分小于千位的
if length <= 4:
self.pre.append(self.num_integer)
# print("整数部分小于4位的:",self.pre)
else:
# 整数部分大于千位的
extra = length % 4
if extra != 0:
self.pre.append(self.num_integer[0: extra])
length = length - extra
start, end = extra, 4 + extra
else:
start, end = 0, 4
while length:
self.pre.append(self.num_integer[start: end])
start += 4
end += 4
length -= 4
# print("整数部分大于4位的:", self.pre)
def per_conver(self, num):
length = len(num)
flag = True
# Special
# deal with '0000'
if num == '0000':
return ''
# deal with '1000'
if num[1:] == '000':
return dictionary[num[0]] + level2[4]
# Normal
res, p = '', length
for i in range(0, length):
# deal with prefix is '0'
if (res == '' or res == dictionary[num[i]]) and num[i] == '0':
# only one '0'
if flag:
res += dictionary[num[i]]
flag = False
p -= 1
continue
# deal with '1001'
if i + 1 < length and num[i + 1] == '0' and num[i] == '0':
p -= 1
continue
# deal with '1010' '1110'
if i == length - 1 and num[i] == '0':
continue
res += dictionary[num[i]]
if p > 1 and num[i] != '0':
res += level2[p]
p -= 1
return res
def convert_integer(self):
self.split_num()
self.process_integer()
# print(self.pre)
p = len(self.pre)
for i in self.pre:
per_res = self.per_conver(i)
self.integer_chn += per_res
if p > 0 and per_res != '':
self.integer_chn += level1[p]
else:
self.integer_chn += level1[1]
p -= 1
def convert_float(self):
# print(self.num_float)
if self.num_float == "00":
self.num_float = ""
for i in range(0, len(self.num_float)):
self.float_chn += dictionary[self.num_float[i]]
if (i + 1) <= 2:
self.float_chn += level3[i + 1]
def convert(self):
self.convert_integer()
self.convert_float()
# print(self.float_chn)
if self.float_chn:
return ''.join(self.integer_chn + self.float_chn)+"整"
return ''.join(self.integer_chn) +"整"
def re_num(float_number):
value = re.compile(r'^[-+]?[0-9]+\.?[0-9]+$')
result = value.match(float_number)
return result
if __name__ == '__main__':
while True:
num = input("请输入您想要转的金额:")
result = re_num(num)
if result:
print(DecimalToChn(num).convert())
continue
print("你输入的不是数字,请重新输入")
python教程
小写金额转大写金额Python代码
- Python代码
-
iqiyi视频解析Python代码
最新吾爱大佬分享的一段iqiyi视频解析Python代码,转载分享给大家参考。代码说明m3u8下载部分:pip install m3u8download-hecoter使用需要nodejs项目链接:https://github.com/hecoter/videoParse/tree/main/iqiyipython代码import requestsimport reimpo...
-
python开发一个桌面僵尸宠物代码
python开发一个桌面行走的僵尸宠物代码,可切换僵尸皮肤,效果如下:python代码截图python代码如下# *_* coding : UTF-8 *_*# author : Leemamas# 开发时间 : 2021/5/28 0:48 import sysfrom PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt...
-
获取免费的https代理Python代码
前言大家用Python爬网页时候,爬快了被封IP,爬慢了,等的着急,这时候就需要https代理来切换IP了。分享一段获取免费的https代理Python代码,可以快速获取网络上免费的https代理。Python代码from multiprocessing.dummy import Lockimport reimport requestsi...
-
基于百度API文字转语音Python示例代码
准备工作1、首先需要去百度智能云注册账号,官网:https://cloud.baidu.com/2、登陆进入百度语音,领取免费资源3、创建应用,记下自己的 APIkey和 Secret Key写入代码对应的位置Python示例代码# coding=utf-8 import sysimport json # 保证兼容python2以及p...
-
微博自定义批量取消关注Python代码
用过微博都知道,微博自带的批量取消关注功能非常不好用,有跟没有一样,还是要一个个点,麻烦的一比。花了半小时写了微博自定义批量取消关注Python代码,用作批量取消关注,如果有部分,不想取消掉,还可以在白名单里设置。import requestsfrom jsonpath import js...
-
01Playwright闲鱼智能监控机器人项目 4个月前
-
02Python汉字笔顺图及书写gif动画生成代码 6个月前
-
03Python获取彩云天气实时天气API源码 1年前
-
04基于Django的RustDesk Api&Web Server源码分享 1年前
-
05批量修改照片文件大小Python脚本 1年前
-
01123网盘解析下载python脚本 724热度
-
02Python自动下载歌曲宝音乐和歌词脚本 472热度
-
03Python和彩云自动签到云函数脚本分享 425热度
-
04Python无需认证QQ扫码登录脚本 424热度
-
05python爬虫下载抖音用户所有短视频+无水印方法 353热度
