Python converts renminbi to uppercase script code _python

Source: Internet
Author: User
Tags lowercase ord
Copy Code code as follows:

def num2moneyformat (Change_number):
"""
. Convert number to uppercase currency format (format_word.__len__ ()-3 + 2 decimal)
Change_number supports float, int, long, string
"""
Format_word = ["min", "Jiao", "Yuan",
"Pick up", "hundred", "thousand", "Million",
"Pick up", "hundred", "thousand", "billion",
"Pick up", "hundred", "thousand", "Million",
"Pick up", "hundred", "thousand", "trillion"]

Format_num = [0], "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine"]
If Type (change_number) = = str:
#-If it is a string, try converting to float or int first.
If '. ' In Change_number:
Try:change_number = float (change_number)
Except:raise valueerror, '%s ' can\ ' t change '%change_number
Else
try:change_number = Int (change_number)
Except:raise valueerror, '%s ' can\ ' t change '%change_number

If Type (change_number) = = float:
Real_numbers = []
For I in range (len (Format_word)-3,-3,-1):
If Change_number >= 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 #标记连续0次数 To delete million words, or insert 0 words in a timely manner
start = Len (real_numbers)-3
Change_words = []
For I in range (start, -3,-1): #使i对应实际位数, negative numbers are corner points
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): #控制 million/yuan
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 "corner," then fill the "whole"
Change_words.append ("whole")

Return '. Join (Change_words)


Python converts the amount lowercase to uppercase 2

Feature converts lowercase amounts less than 10 trillion yuan to uppercase

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)

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.