Python script code for converting RMB into uppercase

Source: Internet
Author: User
Python converts RMB into uppercase code. if you need it, refer The code is as follows:


Def Num2MoneyFormat (change_number ):
"""
. Convert the number to the upper-case currency format (format_word. _ len _ ()-3 + 2 decimal places)
Change_number supports float, int, long, string
"""
Format_word = ["points", "corner", "Yuan ",
"Pick Up", "hundred", "thousand", "Ten Thousand ",
"Pick Up", "hundreds", "thousands", "hundreds of millions ",
"Pick Up", "hundred", "thousand", "Ten Thousand ",
"Pick Up", "hundreds", "thousands", "megabytes"]

Format_num = ["zero", "one", "two", "three", "si", "Wu", "Lu", "yellow", "yellow ", "Success"]
If type (change_number) = str:
#-If it is a string, first try to convert it to float or int.
If '.' in change_number:
Try: change_number = float (change_number)
Warning t: raise ValueError, '% s can \' t Chang' % change_number
Else:
Try: change_number = int (change_number)
Warning t: raise ValueError, '% s can \' t Chang' % change_number

If type (change_number) = float:
Real_numbers = []
For I in range (len (format_word)-3,-3,-1 ):
If change_number> = 10 ** I or I <1:
Real_numbers.append (int (round (change_number/(10 ** I), 2) % 10 ))

Elif isinstance (change_number, (int, long )):
Real_numbers = [int (I) for I in str (change_number) + '00']

Else:
Raise ValueError, '% s can \'t change' % change_number

Zflag = 0 # Mark 0 consecutive times to delete ten thousand characters, or insert zero words in due time
Start = len (real_numbers)-3
Change_words = []
For I in range (start,-3,-1): # set I to the actual number of digits, and the negative number is the angular score.
If 0 <> real_numbers [start-I] or len (change_words) = 0:
If zflag:
Change_words.append (format_num [0])
Zflag = 0
Change_words.append (format_num [real_numbers [start-I])
Change_words.append (format_word [I + 2])

Elif 0 = I or (0 = I % 4 and zflag <3): # control RMB 1 million
Change_words.append (format_word [I + 2])
Zflag = 0
Else:
Zflag + = 1

If change_words [-1] not in (format_word [0], format_word [1]):
#-The last two digits are not "angles". if the score is "points", the value is "integral"
Change_words.append ("whole ")

Return ''. join (change_words)



Python converts the amount to uppercase 2 in lower case

Converts the lower-case amount less than 100,000 billion yuan into upper-case

The 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.