|
代码我给你贴上来,无聊花了点脑力想算法。。。不够优化,也可能有BUG,没测试几次,。。。
public class ddddddddddddd {
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(formatString("10042300456"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 格式化字符串
* @param string 10042300456
* @return
* @throws Exception
*/
private static String formatString(String string) throws Exception {
String string1 = translate(string); //译文:一零零四二三零零四五六
String newString = changeSerial(string1); //颠倒后的序列:六五四零零三二四零零一
char[] newChar = newString.toCharArray();
StringBuffer newString2 = new StringBuffer();
for (int i = 0, length = newChar.length; i < length; i++) { //格式化字符串:六五四零,零三二四,零零一
if ((i != 0) && (i%4 == 0)) {
newString2.append(",");
}
newString2.append(newChar[i]);
}
String newString3 = changeSerial(newString2.toString()); //再次颠倒序列:一零零,四二三零,零四五六
String[] stringArray = newString3.split(",");
for (int i = 0, length = stringArray.length; i < length; i++) {
//为数组内的每个字符串加上单位,并去掉多余的零
stringArray[i] = formart(stringArray[i]); //一百,四千二百三十,零四百五十六
}
int length = stringArray.length;
for (int i = length - 2; i >= 0; i--) {
if (i == length - 2) {
stringArray[i] = stringArray[i] + "万";
continue;
}
if (i == length - 3) {
stringArray[i] = stringArray[i] + "亿";
continue;
}
}
StringBuffer returnString = new StringBuffer();
for (int i = 0; i < length; i++) {
returnString.append(stringArray[i]);
}
return returnString.toString();
} |
|