狂自私

导航

Python 中文数字转阿拉伯数字

#只能转数字,传参中包含非数字会错.
def t(str):
	zhong={'零':0,'一':1,'二':2,'三':3,'四':4,'五':5,'六':6,'七':7,'八':8,'九':9};
	danwei={'十':10,'百':100,'千':1000,'万':10000};
	num=0;
	if len(str)==0:
		return 0;
	if len(str)==1:
		if str == '十':
			return 10;
		num=zhong[str];
		return num;
	temp=0;
	if str[0] == '十':
		num=10;
	for i in str:
		if i == '零':
			temp=zhong[i];
		elif i == '一':
			temp=zhong[i];
		elif i == '二':
			temp=zhong[i];
		elif i == '三':
			temp=zhong[i];
		elif i == '四':
			temp=zhong[i];
		elif i == '五':
			temp=zhong[i];
		elif i == '六':
			temp=zhong[i];
		elif i == '七':
			temp=zhong[i];
		elif i == '八':
			temp=zhong[i];
		elif i == '九':
			temp=zhong[i];
		if i == '十':
			temp=temp*danwei[i];
			num+=temp;
		elif i == '百':
			temp=temp*danwei[i];
			num+=temp;
		elif i == '千':
			temp=temp*danwei[i];
			num+=temp;
		elif i == '万':
			temp=temp*danwei[i];
			num+=temp;
	if str[len(str)-1] != '十'and str[len(str)-1] != '百'and str[len(str)-1] != '千'and str[len(str)-1] != '万':
		num+=temp;
	return num;

  

posted on 2019-04-08 22:11  狂自私  阅读(4441)  评论(0编辑  收藏  举报