Copy codeThe Code is as follows :#! /Usr/bin/python
#-*-Encoding: UTF-8 -*-
Import types
Class NotIntegerError (Exception ):
Pass
Class OutOfRangeError (Exception ):
Pass
_ MAPPING = (u '0', u '1', u '2', u '3', u '4', u '5', u '6 ', u '7', u '8', u '9 ',)
_ P0 = (u'', u'10', u'100', u'kil ',)
_ S4, _ S8, _ S16 = 10 ** 4, 10 ** 8, 10 ** 16
_ MIN, _ MAX = 0, 9999999999999999
Def _ to_chinese4 (num ):
''''' Converts the Arabic numerals between 0 and 10000.
'''
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) # digits
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'0'
Return result [:-1]. replace (u'110', u'10 ')
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'wan'
Else:
If low <_ S4/10:
Return to4 (high) + u'ten thousand 0' + to4 (low)
Else:
Return to4 (high) + u'wan' + 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'100'
Else:
If low <_ S8/10:
Return to8 (high) + u 'yi0' + to8 (low)
Else:
Return to8 (high) + u'100 '+ 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)
Converts the amount in lower case to a large Python code.
Converts the lower-case amount less than 100,000 billion yuan into upper-case
CodeCopy codeThe Code is as follows: def IIf (B, s1, s2 ):
If B:
Return s1
Else:
Return s2
Def num2chn (nin = None ):
Cs =
('0', 'yi', 'er', 'san', 'siz', 'wu', 'lu', 'taobao', 'taobao ', 'taobao', '◇ ', 'quantity', 'angular', 'circle ', 'scuse', 'taobao', 'taobao ',
'Wan', 'pick', 'taobao', 'taobao', 'yiyi', 'pick', 'taobao', 'wan ')
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, '', 'rounded '))
+ St1
Fg = (ns = 0)
St. replace ('billions ', 'wan ')
Return IIf (nin = 0, '0', st + st1)
If _ name _ = '_ main __':
Num = 12340.1
Print num
Print num2chn (num)