Java Enables you to convert Arabic numerals to Chinese number 123 = "123

Package Penalty for DAY1; 

Import java.util.ArrayList;
 Import java.util.Collections; 

public  class the Test { 
    
     / **  
     * units carry, Chinese defaults to 4, ie (million and one hundred million) 
     * /   
    public  int UNIT_STEP = 4 ;   
  
    / **  
     * unit 
     * /   
    public String [] CN_UNITS = new new String [] { "a", "ten", "100", "thousand", "Wan", "ten" ,  
             "hundred", "thousand", "100 million" "ten", "100", "thousand", "Wan" };  
  
    / **  
     * Characters 
     * /   
    public String [] = CN_CHARS new new String [] { "zero", "one", "two", "three", "four" ,  
            "Five", "six", "seven", "eight" and "Nine" };   
  
    / **  
     * values into Chinese string 
     *   
     * @param NUM 
     * need to convert the value 
     * @return  
     * /   
    public String the CVT ( Long NUM) {  
         return  the this .cvt (NUM, to false );   
    }   
  
    / **  
     * value is converted into a string Chinese (colloquial) 
     *   
     * @param NUM 
     * value to be converted 
     * @param isColloquial 
     * if colloquial. 12, for example, converted into 'second' rather than 'twelve' 
       
    CVT String ( Long NUM, Boolean isColloquial) {   
        String [] Result = the this .convert (NUM, isColloquial);   
        the StringBuffer STRs = new new the StringBuffer (32 );  
         for (STR String: Result) {   
            strs.append (STR);   
        }   
        return strs.toString ();   
    }   
  
    / **  
     * the values into Chinese 
     *   
     * @param NUM 
     * value to be converted 
     * @param isColloquial 
     * if colloquial. 12, for example, converted into 'second' rather than 'twelve'. 
     * @return  
     * /   
    publicString [] Convert ( Long NUM, Boolean isColloquial) {  
         IF (NUM <10) { // 10 characters or less directly returns the corresponding   
            return  new new String [] {CN_CHARS [( int ) NUM]}; // ASCII2int   
        }   
  
        char [] chars = String.valueOf (NUM) .toCharArray ();  
         IF (chars.length> CN_UNITS.length) { // over a range of units returned empty   
            return  new new String [] {};   
        }   
  
        Boolean isLastUnitStep = to false ; // record subunits carry   
        ArrayList <String> cnchars =new new the ArrayList <String> (chars.length * 2); // create an array, the number placed in the position corresponding to the unit   
        for ( int POS = chars.length -. 1; POS> = 0; pos--) { // from low-order loop   
            char CH = chars [POS];   
            String cnChar = CN_CHARS [CH - '0']; // ascii2int characters   
            int unitPos = chars.length - POS -. 1; // unit corresponding coordinates   
            String cnUnit = CN_UNITS [ unitPos]; // unit   
            Boolean IsZero = (CH == '0'); // whether 0   
            Boolean isZeroLow = (POS. 1 + <chars.length && chars [+ POS. 1] == '0'); // whether low as 0  
  
             Boolean isUnitStep = (unitPos> = UNIT_STEP && (unitPos% UNIT_STEP == 0)); // current bit units need to carry   
  
            IF (isUnitStep && isLastUnitStep) { // removal unit adjacent in a carry   
                int size = cnchars.size ();   
                cnchars.remove (size -. 1 );  
                 IF - {(CN_CHARS [0] .equals (cnchars.get (size 2))!) // fill 0   
                    cnchars.add (CN_CHARS [0 ]) ;   
                }   
            }   
  
            IF (isUnitStep || IsZero!) { // units carry (million and million), or non-add units when 0   
                cnchars.add (cnUnit);   
                isLastUnitStep = isUnitStep;  
            }   
            IF (IsZero && (|| isZeroLow isUnitStep)) { // this low bit is 0 is 0, or a is 0 and the current bit is omitted when the unit carry   
                Continue ;   
            }   
            cnchars.add (cnChar);   
            isLastUnitStep = to false ;   
        }   
  
        Collections.reverse (cnchars);   
        // clear the last digit 0   
        int chSize = cnchars.size ();   
        String CHEND = cnchars.get (chSize - 1 );  
         IF (CN_CHARS [0] .equals (CHEND) || CN_UNITS [0 ] .equals (CHEND)) {   
            cnchars.remove (chSize--1 );   
        }  
  
        // colloquial process   
        IF (isColloquial) {   
            String chFirst = cnchars.get (0 );   
            String chSecond = cnchars.get (. 1 );  
             IF (chFirst.equals (CN_CHARS [. 1]) && chSecond.startsWith (CN_UNITS [. 1] )) { // whether the 'a' at the beginning, followed '10 '   
                cnchars.remove (0 );   
            }   
        }   
        return cnchars.toArray ( new new String [] {});   
    }   
  
    public  static  void main (String [] args) {   
        String [] testData = ( "0,1,2,3,4,5,6,7,8,9,"  
                + "10,11,20,91,"  
                + "110,102,1234,1023,1002,"  
                + "10000,12345,10234,10023,10002,12034,12003,12000,12300,12340,"  
                + "100000,123456,102345,200234,100023,100002,120345,120034,120003,120000,123000,123400,123450,"  
                + "1000000,1234567,2023456,1002345,3000234,1000023,1000002,1203456,1200345,1200034,1200003,1200000,1230000,1234000,1234500,1234560,"  
                + "10000000,12345678,10234567,10023456,10002345,10000234,10000023,12034567,12003456,12000345,12000034,12000003,12000000,12300000,12340000,12345600,12345670,"  
                + "1000000000,1234567890,1023456789,1002300078,1000230067,1000023456,1000002345,1203456789,1200345678,1200034567,1200003456,1200000345,1200000034,1200000003,1230000000,1234000000,1234500000,1234560000,1234567000,1234567800,1234567890,"  
  
                + String.valueOf(Integer.MAX_VALUE)  
  
        + ",9223372036854,9002233007200").split(",");  
        for (String data : testData) {  
            long num = Long.parseLong(data);  
  
            if (num < 0) {  
                continue;  
            }  
  
            System.out.println(String.format("%-12s \t %s", data,  
                    new test().cvt(num,true)));  
  
        }  
  
    }  
        
}

 

Guess you like

Origin www.cnblogs.com/zengcongcong/p/11200399.html