阿拉伯数字转换为中文

  • 实例1:
#-*-coding:utf-8-*-
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)
  • 实例2:
#-*-coding:utf-8-*-
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。'))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值