當前位置:成語大全網 - 書法字典 - Java的中文字數與阿拉伯數字的轉換問題(eclipse版)

Java的中文字數與阿拉伯數字的轉換問題(eclipse版)

包test.format導入Java . text . number format;導入Java . util . hashmap;公共類SimpleMoneyFormat {

公共靜態最終字符串EMPTY =““;公共靜態最終字符串ZERO =“ZERO“;公共靜態最終字符串ONE =“ONE“;公共靜態最終字符串TWO =“TWO“;public static final String THREE =“THREE”;公共靜態最終字符串FOUR =“Si“;公共靜態最終字符串FIVE =“Wu“;公共靜態最終字符串SIX =“land“;公共靜態最終字符串SEVEN =“Qi“;Public static final String EIGHT =“八“;公共靜態最終字符串NINE =“NINE“;Public static最終字符串TEN =“pick up“;public static final string forgotten =“bain“;Public static final string千=“千“;public static final string ten _ past =“萬“;Public static final字符串ended _ million =“億“;Public static最終字符串YUAN =“YUAN“;public static final String JIAO =“angle”;公共靜態最終字符串FEN =“points“;公共靜態最終字符串DOT =“。“;私有靜態SimpleMoneyFormat格式化程序= nullprivate HashMap chineseNumberMap = new HashMap();private HashMap chinesemoney pattern = new HashMap();private NumberFormat數字格式=數字格式。getinstance();private simplemoney format(){ number format . setmaximumfractiondigits(4);number format . setminimumfractiondigits(2);number format . setgroupingused(false);chinesenumbermap . put(“0“,零);chinesenumbermap . put(“1“,ONE);chinesenumbermap . put(“2”,TWO);chinesenumbermap . put(“3”,THREE);chinesenumbermap . put(“4”,FOUR);chinesenumbermap . put(“5”,FIVE);chinesenumbermap . put(“6”,SIX);chinesenumbermap . put(“7”,SEVEN);chinesenumbermap . put(“8”,八);chinesenumbermap . put(“9”,九);chineseNumberMap.put(點,點);chinesemoney pattern . put(“1“,TEN);chinesemoney pattern .(“2”,百);chinesemoney pattern .(“3”,千);chinesemoney pattern . put(“4“,TEN _ found);chinesemoney pattern(“5”,TEN);chinesemoney pattern .(“6”,百);chinesemoney pattern .(“7”,千);chinesemoney pattern . put(“8“,bail _ MILLION);}

public static simplemoney format getInstance(){ if(formatter = = null)formatter = new simplemoney format();返回格式化程序;}

公共字符串格式(String money str){ check precision(money str);字符串結果;result = convertToChineseNumber(money str);result = addunitstochinesemoney string(result);返回結果;}公共字符串格式(double money double){返回格式(number format . format(money double));}公共字符串格式(int money int){返回格式(number format . format(money int));

}公共字符串格式(long money long){返回格式(number format . format(money long));}

公共字符串格式(Number money num){返回格式(Number format . format(money num));}

私有字符串convertToChineseNumber(String money str){ String result;string buffer cMoneyStringBuffer = new string buffer();for(int I = 0;我& ltmoney str . length();i++){ cmoneystringbuffer . append(chinesenumbermap . get(money str . substring(I,I+1)));}

//幾百萬億等等都是漢字中的單位,加上它們的int索引dot = cmoneystringbuffer。(點)的索引;int money pattern cursor = 1;for(int I = indexOfDot-1;我& gt0;I-){ cmoneystringbuffer . insert(I,chinesemoneypattern . get(EMPTY+money pattern cursor));moneyPatternCursor = moneyPatternCursor = = 8?1:money pattern cursor+1;} String fraction part = cmoneystringbuffer . substring(cmoneystringbuffer . index of(“。“));cmoneystringbuffer . delete(cmoneystringbuffer . index of(“。“)、cmoneystringbuffer . length());while(cmoneystringbuffer。indexof(“零“)!= -1) {

Cmoneystringbuffer。replace(cMoneyStringBuffer . index of(“zero“),cmoneystringbuffer。索引(“零“)+2,零);

} while(cmoneystringbuffer。indexof(“零百“)!= -1) {

Cmoneystringbuffer。replace(cMoneyStringBuffer . index of(“零百“),cmoneystringbuffer。指數(“零百“)+2,零);

} while(cmoneystringbuffer。indexof(“零千“)!= -1) {

Cmoneystringbuffer。replace(cMoneyStringBuffer . index of(“零千”),cmoneystringbuffer。指數(“零千”)+2,零);

} while(cmoneystringbuffer。indexof(“萬“)!= -1) {

Cmoneystringbuffer。replace(cMoneyStringBuffer . index of(“萬”),cmoneystringbuffer。index of(“萬“+2,ten _ question);

} while(cmoneystringbuffer。indexof(“零億“)!= -1) {

Cmoneystringbuffer。replace(cMoneyStringBuffer . index of(“零億”),cmoneystringbuffer。指數(“零億“)+2,億);} while(cmoneystringbuffer。indexof(“零零“)!= -1) {

Cmoneystringbuffer。replace(cMoneyStringBuffer . index of(“zero zero“),cmoneystringbuffer。索引(“零零“)+2,零);

} if(cmoneystringbuffer . lastindexof(ZERO)= = cmoneystringbuffer . length()-1)cmoneystringbuffer . delete(cmoneystringbuffer . length()-1,cmoneystringbuffer . length());cmoneystringbuffer . append(fraction part);

result = cmoneystringbuffer . tostring();返回結果;}私有字符串addunitstochinesemoney String(String money str){ String result;string buffer cMoneyStringBuffer = new string buffer(money str);int index of DOT = cmoneystringbuffer . index of(DOT);cmoneystringbuffer . replace(indexOfDot,indexOfDot + 1,YUAN);cmoneystringbuffer . insert(cmoneystringbuffer . length()-1,JIAO);cmoneystringbuffer . insert(cmoneystringbuffer . length()、FEN);if(cmoneystringbuffer . index of(“零角度零點“)!=-1)//沒有分數,四舍五入。

Cmoneystringbuffer。替換(cmoneystringbuffer。index of(“零角度和零點”),cMoneyStringBuffer.length(),“整體”);else if(cmoneystringbuffer。indexof(“零“)!=-1)//如果沒有零點,則添加cmoneystringbuffer。替換(cmoneystringbuffer。index of(“零點“),cMoneyStringBuffer.length(),“整數“);

else { if(cmoneystringbuffer。indexof(“零角度“)!=-1)

cmoneystringbuffer . delete(cMoneyStringBuffer.indexOf(“零度角“),cmoneystringbuffer . index of(“零度角“)+2);

tmpbuffer . append(“whole“);} result = cmoneystringbuffer . tostring();返回結果;}私有void check precision(String money str ){

int fraction digits = money str . length()-money str . index of(DOT)-1;if(fraction digits & gt;2)throw new runtime exception(“amount“+money str+“的小數位數超過兩位。);//精度不能再低了} public static void main(string args【】){ system。out . println(getinstance()。格式(新雙精度型(1001001.065438));}}