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;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM