import math
chineseNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
chineseUnit1 = ["", "万", "亿", "万亿", "亿亿"];
chineseUnit2 = ["", "十", "百", "千"];
def Section2Chinese(number):
strIns = ''
chnStr = ''
unitPos = 0
zero = True
while number>0:
v = int(number % 10)
if v == 0:
if not zero:
zero = True
chnStr = chineseNum[v] + chnStr
else:
zero = False
strIns = chineseNum[v]
strIns += chineseUnit2[unitPos]
chnStr = strIns + chnStr
unitPos = unitPos + 1
number = math.floor(number/10)
return chnStr
def Number2Chinese(num):
unitPos = 0
strIns = ''
chnStr = ''
needZero = False
if (num == 0):
return chnNumChar[0]
while (num > 0):
section = num % 10000;
if (needZero):
chnStr = chnNumChar[0] + chnStr
strIns = Section2Chinese(section)
if section != 0:
strIns = strIns + chineseUnit1[unitPos]
else:
strIns = strIns + chineseUnit1[0]
chnStr = strIns + chnStr;
needZero = (section < 1000) & (section > 0)
num = math.floor(num / 10000);
unitPos = unitPos + 1
return chnStr
if __name__ == '__main__':
print Number2Chinese(12345678910.3)
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import glob
import codecs
import re,os
import soundfile
import json
import math
chineseNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
chineseUnit1 = ["", "万", "亿", "万亿", "亿亿"];
chineseUnit2 = ["", "十", "百", "千"];
def replace_decimal(text):
for d,a_char in enumerate(chineseNum):
text = text.replace(str(d),a_char)
text = text.replace('.','点')
return text
def Process_string(text):
Preprocess_texts = re.findall(u'[0-9.%]+[\u4e00-\u9fa5,。]',text)
print(Preprocess_texts)
if Preprocess_texts == []:
return text
for pre_text in Preprocess_texts:
percent_flag = False
if '%' in pre_text:
percent_flag = True
number_t = re.sub('[^0-9.]','',pre_text)
tail_t = pre_text[-1]
decimal_t = ''
if '.' in number_t:
decimal_t = number_t[number_t.index('.'):]
decimal_t = replace_decimal(decimal_t)
if percent_flag:
temp = '百分之' + Number2Chinese(float(number_t)) + decimal_t + tail_t
text = text.replace(pre_text,temp)
else:
temp = Number2Chinese(float(number_t)) + decimal_t + tail_t
text = text.replace(pre_text,temp)
return text
def Section2Chinese(number):
strIns = ''
chnStr = ''
unitPos = 0
zero = True
while number>0:
v = int(number % 10)
if v == 0:
if not zero:
zero = True
chnStr = chineseNum[v] + chnStr
else:
zero = False
strIns = chineseNum[v]
strIns += chineseUnit2[unitPos]
chnStr = strIns + chnStr
unitPos = unitPos + 1
number = math.floor(number/10)
return chnStr
def Number2Chinese(num):
unitPos = 0
strIns = ''
chnStr = ''
needZero = False
if (int(num) == 0):
return chineseNum[0]
while (num > 0):
section = num % 10000;
if (needZero):
chnStr = chineseNum[0] + chnStr
strIns = Section2Chinese(section)
if section != 0:
strIns = strIns + chineseUnit1[unitPos]
else:
strIns = strIns + chineseUnit1[0]
chnStr = strIns + chnStr;
needZero = (section < 1000) & (section > 0)
num = math.floor(num / 10000);
unitPos = unitPos + 1
return chnStr
if __name__ == "__main__":
print(Process_string(u'公元300年前,20年前,你们101我,20,2040梅花。1998年12月30日10时05分10秒,0.2334。20我,30门,20。'))