Package test; Import java. text. DecimalFormat;
Public class Test { Private static final String digit_0 = ""; Private static final String digit_1 = "0 August 5, 1234 "; Private static final String [] digit_2 = {"", "10", "hundreds", "thousands "}; Private static final String [] digit_3 = {"", "Pick", "success", "success "}; Private static final String [] digit_4 = {"", "trillion "};
/** * @ Param args */ Public static void main (String [] args ){ System. out. println (changeDigit ("2012.12", false )); System. out. println (changeDigit ("210.12", true )); System. out. println (changeNumberRMB (210.12 )); }
/** * Description: converts a number to an integer. * @ Param str * @ Param bo * @ Return */ Public static String changeDigit (String str, boolean bo ){ StringBuffer strbu = new StringBuffer (); Int dou = str. indexOf ("."); //: Determines whether it is a decimal or an integer. The length less than zero is an integer. If (dou <0 ){ Dou = str. length (); } //: Obtain the integer part. String inter = str. substring (0, dou ); Strbu. append (changeInteger (Long. parseLong (inter), bo )); //: Process the fractional part. If (dou! = Str. length ()){ Strbu. append ("point "); //: Returns the number of decimal places. String xh = str. substring (dou + 1 ); For (int I = 0; I <xh. length (); I ++ ){ If (bo ){ Strbu. append (digit_0.charAt (Integer. parseInt (xh. substring ( I, I + 1 )))); } Else { Strbu. append (digit_1.charAt (Integer. parseInt (xh. substring ( I, I + 1 )))); } } } String strs = strbu. toString (); // Handle special cases, which may be incomplete If (strs. startsWith ("0 ")){ Strs = strs. substring (1 ); } If (strs. startsWith ("10 ")){ Strs = strs. substring (1 ); } While (strs. endsWith ("0 ")){ Strs = strs. substring (0, strs. length ()-1 ); } If (strs. startsWith ("point ")){ Strs = "zero" + strs; } If (strs. endsWith ("point ")){ Strs = strs. substring (0, strs. length ()-1 ); } Return strs; }
/** * When the number of digits is less than 4, data processing is called. * * @ Param str * @ Param bo * @ Return */ Public static String readNumber (String str, boolean bo ){ StringBuffer strbu = new StringBuffer (); If (str. length ()! = 4 ){ Return null; } For (int I = 0; I <4; I ++ ){ Char ch = str. charAt (I ); If (ch = '0' & I> 1 & str. charAt (I-1) = '0 '){ Continue; } If (ch! = '0' & I> 1 & str. charAt (I-1) = '0 '){ Strbu. append ('0 '); } If (ch! = '0 '){ If (bo ){ Strbu. append (digit_0.charAt (ch-48 )); Strbu. append (digit_3 [4-I-1]); } Else { Strbu. append (digit_1.charAt (ch-48 )); Strbu. append (digit_2 [4-I-1]); } } } Return strbu. toString (); }
/** * The integer part is converted to uppercase. * * @ Param lon * @ Param bo * @ Return */ Public static String changeInteger (long lon, boolean bo ){ StringBuffer strbu = new StringBuffer (); //: Increase the number of digits by 3. String strN = "000" + lon; Int strN_L = strN. length ()/4; //: Removes the number of strN "0" based on the length of different digits. StrN = strN. substring (strN. length ()-strN_L * 4 ); For (int I = 0; I <strN_L; I ++ ){ String s1 = strN. substring (I * 4, I * 4 + 4 ); String s2 = readNumber (s1, bo ); Strbu. append (s2 ); If (s2.length ()! = 0 ){ Strbu. append (digit_4 [strN_L-I-1]); } } String s = new String (strbu ); If (s. length ()! = 0 & s. startsWith ("0 ")) S = s. substring (1 ); Return s; }
/** * Name currency output format * * @ Param RMB * @ Return */ Public static String changeNumberRMB (double RMB ){ String strRMB = "" + RMB; DecimalFormat df = new DecimalFormat ("#.#"); Df. setMaximumFractionDigits (2 ); StrRMB = df. format (RMB). toString (); //: True in uppercase and return data StrRMB = changeDigit (strRMB, true ); If (strRMB. indexOf ("point")> = 0 ){ StrRMB = strRMB + "zero "; StrRMB = strRMB. replaceAll ("point", "circle "); String str1 = strRMB. substring (0, strRMB. indexOf ("circle") + 1 ); String str2 = strRMB. substring (strRMB. indexOf ("circle") + 1 ); StrRMB = str1 + str2.charAt (0) + "" + str2.charAt (1) + ""; } Else { StrRMB = strRMB + "round "; } Return "RMB (uppercase):" + strRMB; }
}
|