Java floating-point-to-renminbi reading method
- The "integer part" of the number, starting from the single digit, 4 numbers are divided into a group, the highest bit no number on 0, and record the total number of arrays, a total of 3 groups, also 3*4 two-dimensional array;
- For each array of uniform processing, and finally the most beginning of the "0 minus", "Million", "million" "Yuan" before the word "0" removed;
- When reading to 0 o'clock, check if the previous one is 0, if the previous one is 0, then do not handle, if the previous one is not 0 or the previous one does not exist, all note "0";
- Read each number, converted to the corresponding uppercase Chinese characters, and add the weight "thousand, Bai, pick up", according to read the first few arrays, followed by the corresponding "million", "million" "Yuan";
- The "fractional part" to do one-dimensional array processing, only two decimal places, corresponding to the "angle" and "minute", the number 0 is not processed;
The test number "120, 3005, 7600.89", read: One Bai hundred billion three thousand Wu Wu Qian seven thousand yuan Ba angle nine points.
Packagethetest; Public classNUMTORMB {Privatestring[] hanarr={"0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "JIU"}; PrivateString[] unitarr={"Qian", "Bai", "Pick up"}; /*** Decomposition of a floating-point number into an integer part and a fractional part string *@paramNumber of floating-point numbers that NUM needs to be decomposed *@returnthe decomposed integer and fractional parts, the 1th array element is the integer part, and the 2nd element of the array is the fractional part. */ PrivateString[] Divide (Doublenum) { //casts a floating-point number to a long type, that is, the integer part that gets it LongZheng= (Long) num; //The floating-point number minus the integer part, gets the fractional part, multiplies the fraction by 100, and gets two decimal places. LongXiao=math.round ((Num-zheng) *100); //Two methods are used here to convert integers to strings return NewString[]{string.valueof (Zheng), xiao+ ""}; } /*** The integer is divided into 4 numbers for a set of string processing, converted to uppercase numbers and then connect them together to get the final renminbi reading method *@paramnum requires a floating-point number that is converted to uppercase numbers *@returnreturn the entire number of renminbi reading method*/ PrivateString Conhanstr (Doublenum) { //Storing integer results, fractional resultsString reszheng= ""; String Resxiao=""; //storing the final resultString result= ""; //store integer and fractional part strings, respectivelyString zhengstr=divide (num) [0]; String Xiaostr=divide (num) [1]; /*processing of the decimal part*/ if(Xiaostr.length () ==1) {Xiaostr= "0" +Xiaostr; } Resxiao=Xiao2han (XIAOSTR); /*processing of integer parts*/ //number of records string, which is the first array intCountarr=0; String TempStr=""; //number of record bits intCountnum=0; for(intI=zhengstr.length () -1;i>=0;i--) {Countnum++; TempStr=zhengstr.charat (i) +TempStr; if(i-1<0){ //I have been the last number, not enough bit, to complement 0 while(Tempstr.length () <4) {TempStr= "0" +TempStr; } Countarr++; Reszheng=zheng2han (COUNTARR,TEMPSTR) + "" +Reszheng; }Else{ if(countnum==4) {Countarr++; Reszheng=zheng2han (COUNTARR,TEMPSTR) + "" +Reszheng; Countnum=0; TempStr=""; } } } //Remove the beginning of the 0 if(Reszheng.charat (0) = = ' 0 ') {Reszheng=reszheng.substring (1, Reszheng.length ()); } /*joins of integers and decimals*/ //represents an integer part of 0 if(Reszheng.charat (0) = = ' Yuan ') {Reszheng= "0" +Reszheng; } result=reszheng+Resxiao; returnresult; } /*** Convert each 4-bit integer group into uppercase digits *@paramThe Count record is the first array (1-3) *@paramthe array to which STR needs to be converted *@returnreturns the array of RMB Read method*/ PrivateString Zheng2han (intcount,string str) {String result=""; for(intj=0;j<4;j++){ intTempnum=str.charat (j)-48; if(tempnum!=0){ if(j!=3) {result+=hanarr[tempnum]+Unitarr[j]; }Else{result+=Hanarr[tempnum]; } }Else{ if((j-1<0) | | (Str.charat (j-1) -48!=0) {result+=Hanarr[tempnum]; } } } //Remove the end of the 0 if(Result.charat (Result.length ()-1) = = ' 0 ') {result=result.substring (0, Result.length ()-1); } //after 0 to add "million, million, yuan, angle, minutes." Switch(count) { Case1: Result+ = "Yuan"; Break; Case2: Result+ = "million"; Break; Case3: Result+ = "billion"; Break; default: System.out.println ("Only numbers less than 9999,9999,9999.99 are supported!" "); return""; } returnresult; } /*** Decimals converted to uppercase numbers *@paramthe array to which STR needs to be converted *@returnreturns the fractional renminbi reading method*/ Privatestring Xiao2han (String str) {string result=""; if(!str.equals ("00")){ for(inti=0;i<2;i++){ intTempnum=str.charat (i)-48; if((tempnum!=0) && (i==0) {result+=hanarr[tempnum]+ "Corner"; } if((tempnum!=0) && (i==1) {result+=hanarr[tempnum]+ "Min"; } } } returnresult; } /*** Main function *@paramargs*/ Public Static voidMain (string[] args) {NUMTORMB nr=NewNUMTORMB (); System.out.println (Nr.conhanstr (Double) 12030060078.95)); }}
Java floating-point-to-renminbi reading method