日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python数字转对应中文_python中将阿拉伯数字转换成中文的实现代码 | 学步园

發(fā)布時間:2025/3/15 python 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数字转对应中文_python中将阿拉伯数字转换成中文的实现代码 | 学步园 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

復制代碼 代碼如下:

#!/usr/bin/python

#-*- encoding: utf-8 -*-

import types

class NotIntegerError(Exception):

pass

class OutOfRangeError(Exception):

pass

_MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', )

_P0 = (u'', u'十', u'百', u'千', )

_S4, _S8, _S16 = 10 ** 4 , 10 ** 8, 10 ** 16

_MIN, _MAX = 0, 9999999999999999

def _to_chinese4(num):

'''''轉(zhuǎn)換[0, 10000)之間的阿拉伯數(shù)字

'''

assert(0 <= num and num < _S4)

if num < 10:

return _MAPPING[num]

else:

lst = [ ]

while num >= 10:

lst.append(num % 10)

num = num / 10

lst.append(num)

c = len(lst) # 位數(shù)

result = u''

for idx, val in enumerate(lst):

if val != 0:

result += _P0[idx] + _MAPPING[val]

if idx < c - 1 and lst[idx + 1] == 0:

result += u'零'

return result[::-1].replace(u'一十', u'十')

def _to_chinese8(num):

assert(num < _S8)

to4 = _to_chinese4

if num < _S4:

return to4(num)

else:

mod = _S4

high, low = num / mod, num % mod

if low == 0:

return to4(high) + u'萬'

else:

if low < _S4 / 10:

return to4(high) + u'萬零' + to4(low)

else:

return to4(high) + u'萬' + to4(low)

def _to_chinese16(num):

assert(num < _S16)

to8 = _to_chinese8

mod = _S8

high, low = num / mod, num % mod

if low == 0:

return to8(high) + u'億'

else:

if low < _S8 / 10:

return to8(high) + u'億零' + to8(low)

else:

return to8(high) + u'億' + to8(low)

def to_chinese(num):

if type(num) != types.IntType and type(num) != types.LongType:

raise NotIntegerError(u'%s is not a integer.' % num)

if num < _MIN or num > _MAX:

raise OutOfRangeError(u'%d out of range[%d, %d)' % (num, _MIN, _MAX))

if num < _S4:

return _to_chinese4(num)

elif num < _S8:

return _to_chinese8(num)

else:

return _to_chinese16(num)

if __name__ == '__main__':

print to_chinese(9000)

把金額小寫轉(zhuǎn)換成大寫的Python代碼

功能將小于十萬億元的小寫金額轉(zhuǎn)換為大寫

代碼

復制代碼 代碼如下:

def IIf( b, s1, s2):

if b:

return s1

else:

return s2

def num2chn(nin=None):

cs =

('零','壹','貳','叁','肆','伍','陸','柒','捌','玖','◇','分','角','圓','拾','佰','仟',

'萬','拾','佰','仟','億','拾','佰','仟','萬')

st = ''; st1=''

s = '%0.2f' % (nin)

sln =len(s)

if sln >; 15: return None

fg = (nin<1)

for i in range(0, sln-3):

ns = ord(s[sln-i-4]) - ord('0')

st=IIf((ns==0)and(fg or (i==8)or(i==4)or(i==0)), '', cs[ns])

+ IIf((ns==0)and((i<>;8)and(i<>;4)and(i<>;0)or fg

and(i==0)),'', cs[i+13])

+ st

fg = (ns==0)

fg = False

for i in [1,2]:

ns = ord(s[sln-i]) - ord('0')

st1 = IIf((ns==0)and((i==1)or(i==2)and(fg or (nin<1))), '', cs[ns])

+ IIf((ns>;0), cs[i+10], IIf((i==2) or fg, '', '整'))

+ st1

fg = (ns==0)

st.replace('億萬','萬')

return IIf( nin==0, '零', st + st1)

if __name__ == '__main__':

num = 12340.1

print num

print num2chn(num)

總結

以上是生活随笔為你收集整理的python数字转对应中文_python中将阿拉伯数字转换成中文的实现代码 | 学步园的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。