Using Python to convert the digital renminbi amount into uppercase script program

Source: Internet
Author: User

#-*-coding:utf-8-*-defNum2moneyformat (change_number):"""convert numbers to uppercase currency format (format_word.__len__ ()-3 + 2-bit decimals) Change_number supports float, int, long, string"""Format_word= ["points","Angle","Yuan",               "Pick","Hundred","thousand","million",               "Pick","Hundred","thousand","billion",               "Pick","Hundred","thousand","million",               "Pick","Hundred","thousand","trillion"] Format_num= ["0"," One","Ii.","three","Restaurant","Wu","Lu","Qi","BA","JIU"]    ifType (change_number) = =Str:#-If it is a string, try converting it to float or int first.        if '.' inchChange_number:Try: Change_number =float (change_number)except:RaiseValueError,'%s can\ ' t change'%Change_numberElse:            Try: Change_number =Int (change_number)except:RaiseValueError,'%s can\ ' t change'%Change_numberifType (change_number) = =float:real_numbers= []         forIinchRange (len (Format_word)-3,-3, 1 ):            ifChange_number >= * * IorI < 1: real_numbers.append (int (round (Change_number/(10**i), 2)%10 ) )    elifisinstance (Change_number, (int, long)): Real_numbers= [Int (i) forIinchSTR (change_number) +'xx' ]    Else:        RaiseValueError,'%s can\ ' t change'%Change_number Zflag= 0#Mark 0 consecutive times to delete the word, or insert 0 words in a timely mannerstart = Len (real_numbers)-3Change_words= []     forIinchRange (Start,-3,-1):#make I correspond to the actual number of digits, negative numbers are the angular points        if0 <> Real_numbers[start-i]orLen (change_words) = =0:ifzflag:change_words.append (format_num[0]) Zflag=0 change_words.append (format_num[real_numbers[start-i]]) change_words.append (format_word[i+2])        elif0 = = Ior(0 = = i%4 andZflag < 3):#control million/yuanChange_words.append (format_word[i+2]) Zflag=0Else: Zflag+ = 1ifCHANGE_WORDS[-1] not inch(Format_word[0], format_word[1]):        #-the last two non-"angle, minute" complement "whole "Change_words.append ("Whole")    return "'. Join (change_words)PrintNum2moneyformat (343.45)

Operation Result: Three hundred restaurants pick up a three-dimensional corner of the woods

Script to convert digital renminbi amounts to uppercase in Python

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.