Copy Code code as follows:
#!/usr/bin/python
#-*-Encoding:utf-8-*-
Import types
Class Notintegererror (Exception):
Pass
Class Outofrangeerror (Exception):
Pass
_mapping = (U ' 0 ', U ' one ', U ' two ', U ' three ', U ' four ', U ' v ', U ' vi ', U ' seven ', U ' eight ', U ' Nine ',)
_p0 = (U ', U ' ten ', U ' hundred ', U ' thousand ',)
_S4, _s8, _S16 = 10 * * * 4, 10 * * 8, 10 * * 16
_min, _max = 0, 9999999999999999
def _to_chinese4 (num):
"" Converts the Arabic numerals between [0, 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) # Number of 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 ' 10 ', U ' Ten ')
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 ' million '
Else
If low < _S4/10:
return to4 (High) + U ' million zero ' + to4 (Low)
Else
return to4 (High) + U ' million ' + 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 ' billion '
Else
If low < _S8/10:
Return To8 (High) + U ' billion zero ' + to8 (Low)
Else
Return To8 (High) + U ' billion ' + 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)
Convert amount lowercase to uppercase Python code
Feature converts lowercase amounts less than 10 trillion yuan to uppercase
Code
Copy Code code as follows:
def IIf (b, S1, S2):
If B:
return S1
Else
Return s2
def num2chn (Nin=none):
CS =
(' 0 ', ' one ', ' II ', ' three ', ' The Shops ', ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU ', ' ◇ ', ' min ', ' jiao ', ' round ', ' pick ', ' bai ', ' Qian ',
' Million ', ' pick ', ' bai ', ' thousand ', ' billion ', ' pick ', ' bai ', ' thousand ', ' million '
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, ', ' whole ')
+ St1
FG = (ns==0)
St.replace (' billions ', ' million ')
Return IIf (nin==0, ' 0 ', St + st1)
if __name__ = = ' __main__ ':
num = 12340.1
Print num
Print Num2chn (num)