在處理財(cái)務(wù)賬款時(shí),,需要將轉(zhuǎn)賬金額寫成大寫的。也就是說,,如果要轉(zhuǎn)賬123456.00元,,則需要寫成“壹拾貳萬叁仟肆佰伍拾陸元整”,。 具體代碼如下: import java.text.DecimalFormat; import java.util.*; //@北冥道人騎鯤打代碼 public class f2 { private static String[] numBig = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; private static String[] numInt = { "", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟" };// 整數(shù)單位 private static String[] numFloat = { "厘", "分", "角" };// 小數(shù)單位 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("請(qǐng)輸入金額"); double money = input.nextDouble(); //格式化double數(shù)字 DecimalFormat df = new DecimalFormat("#0.###");//此時(shí)strNum小數(shù)位最多3位 String strNum = df.format(money); if (strNum.indexOf(".") > 0 ) {//判斷是否有小數(shù) String strMoneyInt = strNum.substring(0, strNum.indexOf(".")); if(strMoneyInt.length() > 12){ System.out.println("數(shù)字太大了,轉(zhuǎn)換不了"); }else{ System.out.println(getInt(strNum) + "元" + getDouble(strNum)); } } else{ if(strNum.length() > 12){ System.out.println("數(shù)字太大了,,轉(zhuǎn)換不了"); }else{ System.out.println(getInt(strNum) + "元整"); } } } //整數(shù)部分 public static String getInt(String str) { if(str.indexOf(".") != -1){ str = str.substring(0,str.indexOf("."));//截取小數(shù)點(diǎn)前面的數(shù)字 } str = new StringBuffer(str).reverse().toString();//反轉(zhuǎn)字符串 StringBuffer strB = new StringBuffer(); //創(chuàng)建一個(gè)空的StringBuffer對(duì)象 for (int i = 0; i < str.length(); i++){ //把單位添加進(jìn)去 strB.append(numInt[i]); strB.append(numBig[(str.charAt(i)-48)]); //str.charAt(i)-48,,這里-48是因?yàn)閟tr.charAt(i)-48為ASCII碼 //而參照ASCII碼: //ASCII碼為 48 ==》0 //ASCII碼為 49 ==》1 ... } str = strB.reverse().toString();//把反轉(zhuǎn)過的字符串還原 //替換字符串多于的字符 if (str.indexOf("零拾") != -1){str = str.replace( "零拾", "零");} if (str.indexOf("零佰") != -1){str = str.replace( "零拾", "零");} if (str.indexOf("零仟") != -1){str = str.replace( "零拾", "零");} if (str.indexOf("零萬") != -1){str = str.replace( "零拾", "萬");} if (str.indexOf("零億") != -1){str = str.replace( "零拾", "億");} if (str.indexOf("零零") != -1){str = str.replace( "零拾", "零");} if (str.indexOf("億萬") != -1){str = str.replace( "零拾", "億");} //除去零的結(jié)尾 if (str.lastIndexOf("零") == str.length() - 1) { str = str.substring(0, str.length() - 1); } return str; } //小數(shù)部分 public static String getDouble(String str) { str = str.substring(str.indexOf(".") + 1);//截取小數(shù)點(diǎn)后的數(shù)字 //解決單位錯(cuò)位 if(str.length() == 1){str = str +"00";} else if(str.length() == 2){str = str +"0";} str = new StringBuffer(str).reverse().toString();//反轉(zhuǎn)字符串 StringBuffer strB = new StringBuffer();//創(chuàng)建一個(gè)空的StringBuffer對(duì)象 for (int i = 0; i < str.length(); i++) {//把單位添加進(jìn)去 strB.append(numFloat[i]); strB.append(numBig[str.charAt(i) - 48]); } str = strB.reverse().toString();//把反轉(zhuǎn)過的字符串還原 //替換字符串多于的字符 if (str.indexOf("零角") != -1){str = str.replace( "零角", "零");} if (str.indexOf("零分") != -1){str = str.replace( "零分", "零");} if (str.indexOf("零厘") != -1){str = str.replace( "零厘", "零");} if (str.indexOf("零零") != -1){str = str.replace( "零零", "零");} //除去零的結(jié)尾 if (str.lastIndexOf("零") == str.length() - 1) { str = str.substring(0, str.length() - 1); } return str; } }
運(yùn)行截圖: |
|