阿拉伯数字转换为中文

  • 实例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。'))

下面是阿拉伯数字转换中文大写数字的代码: ```python def num2cn(num): # 中文大写数字 cn_num = { '0': '零', '1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖' } # 中文数字单位 cn_unit = ['', '拾', '佰', '仟', '万', '亿'] # 将数字转换为字符串并反转 num_str = str(num)[::-1] # 记录是否出现过非零数字 has_value = False # 记录是否需要添加单位 need_unit = False # 记录上一个数字是否为零 prev_zero = False # 中文大写数字字符串 cn_str = '' # 遍历数字字符串 for i in range(len(num_str)): # 当前数字 n = num_str[i] # 当前单位 unit = cn_unit[i] # 如果当前数字为零 if n == '0': # 如果之前没有出现过非零数字,不需要将零添加到字符串中 if not has_value: continue # 如果之前出现过非零数字,并且上一个数字不是零,需要将零添加到字符串中 elif not prev_zero: cn_str += cn_num[n] prev_zero = True # 如果当前数字不为零 else: # 添加单位 if need_unit: cn_str += unit # 添加数字 cn_str += cn_num[n] # 标记出现过非零数字 has_value = True # 标记当前数字不是零 prev_zero = False # 标记需要添加单位 need_unit = True # 如果当前数字已经是最后一位,并且需要添加单位,添加“元”单位 if i == len(num_str) - 1 and need_unit: cn_str += '元' # 将字符串反转并返回 return cn_str[::-1] ``` 使用示例: ```python num = 123456789 cn_num = num2cn(num) print(cn_num) # 输出:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌十九元 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值