Java floating point to RMB

Keywords: Java

Java floating point to RMB

  • The number of [integer part] is divided into four groups starting from the digits. If there is no digit in the highest position, zero will be added. The total number of arrays will be recorded. There are three groups in total, that is, a two-dimensional array of 3 * 4;
  • For the unified processing of each array, the "zero" at the beginning is removed, and the "zero" before the words of "hundred million", "ten thousand" and "Yuan" is removed;
  • When reading 0, check whether its previous bit is 0. If the previous bit is 0, do not process. If the previous bit is not 0 or the previous bit does not exist, record "zero";
  • Read each number, convert it to the corresponding capital Chinese character, and add the weight "thousand, hundred, and ten". According to the read is the first array, add the corresponding "hundred million", "ten thousand" and "Yuan" after it;
  • Do one-dimensional array processing for [decimal part], only two decimal places are reserved, corresponding to "angle" and "minute", and the number 0 is not processed;

 

  
The test number 「 120, 3005, 7600.89 」, read as: one million two billion three million fifty seven thousand six hundred yuan and eighty-nine cents.
 
 
package theTest;
public class NumToRmb {
     
     private String[] hanArr={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
     
     private String[] unitArr={"Thousand","Bai","Ten"};
     
     /**
      * Decomposing a floating-point number into an integer part and a decimal part string
      * @param num Floating point number to be decomposed
      * @return The first array element is the integer part and the second array element is the decimal part.
      */
     private String[] divide(double num){
           //Cast a floating point number to long Type, i.e. get its integer part
           long zheng=(long)num;
           
           //Subtract the integer part from the floating-point number to get the decimal part. Multiply the decimal part by 100 to get two decimal places
           long xiao=Math.round((num-zheng)*100);
           
           //There are two ways to convert integers to strings
           return new String[]{String.valueOf(zheng),xiao+""};
     }
     /**
      * The whole number is divided into four numbers into a group of string processing, converted into capital numbers and then connected to get the final RMB reading method
      * @param num Floating point numbers that need to be converted to uppercase numbers
      * @return Return the whole number in RMB
      */
     private String conHanStr(double num){
           
           //Store integer result and decimal result
           String resZheng="";
           String resXiao="";
           
           //Store final results
           String result="";
           
           //Store integer part and decimal part strings respectively
           String zhengStr=divide(num)[0];
           String xiaoStr=divide(num)[1];
           
           /*Processing of decimal part*/
           if(xiaoStr.length()==1){
                xiaoStr="0"+xiaoStr;
           }
           
           resXiao=xiao2Han(xiaoStr);
           
           /*Processing of integer part*/
           //Number of record strings,Is the array number
           int countArr=0;
           String tempStr="";
           
           //Record digit
           int countNum=0;
           
           for(int i=zhengStr.length()-1;i>=0;i--){
                countNum++;
                tempStr=zhengStr.charAt(i)+tempStr;
                
                if(i-1<0){
                     //i It's the last number. It's not enough. Fill in 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 leading zeros
           if(resZheng.charAt(0)=='Zero'){
                resZheng=resZheng.substring(1,resZheng.length());
           }
           
           /*The connection between integers and decimals*/
           //Indicates that the integer part is 0
           if(resZheng.charAt(0)=='element'){
                resZheng="Zero"+resZheng;
           }
           
           result=resZheng+resXiao;
           
           return result;
     }
     
     /**
      * Each 4-bit integer group is converted to an uppercase number
      * @param count Records are the first array (1-3)
      * @param str Array to be converted
      * @return Return the RMB reading method of the array
      */
     private String zheng2Han(int count,String str){
           
           String result="";
           for(int j=0;j<4;j++){
                int tempNum=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 trailing zeros
           if(result.charAt(result.length()-1)=='Zero'){
                result=result.substring(0, result.length()-1);
           }
           
           //Add "hundred million, ten thousand, yuan, Jiao and Fen" after zero removal.
           switch(count){
           case 1:
                result+="element";
                break;
           case 2:
                result+="ten thousand";
                break;
           case 3:
                result+="Billion";
                break;
           default:
                System.out.println("Only support no more than 999999999.99 The number!");
                return "";
           }
           return result;
     }
     
     /**
      * Decimal to uppercase numbers
      * @param str Array to be converted
      * @return Return the decimal RMB reading method
      */
     private String xiao2Han(String str){
           String result="";
           if(!str.equals("00")){
                for(int i=0;i<2;i++){
                     int tempNum=str.charAt(i)-48;
                     if((tempNum!=0)&&(i==0)){
                           result+=hanArr[tempNum]+"horn";
                     }
                     if((tempNum!=0)&&(i==1)){
                           result+=hanArr[tempNum]+"branch";
                     }
                }
           }
           return result;
     }
     
     /**
      * Main function
      * @param args
      */
     public static void main(String[] args) {
           NumToRmb nr=new NumToRmb();
          System.out.println(nr.conHanStr((double)12030060078.95));
     }
}

 

Posted by Leveecius on Thu, 02 Apr 2020 03:01:38 -0700