#-*-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