class MoneyFormat{ private final String [] pattern ={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"}; private final String [] cPattern ={"","拾","佰","仟","萬","拾","佰","仟","億"}; private final String [] cfPattern = {"","角","分"}; private final String ZEOR = "零"; public MoneyFormat(){ System.out.println("run..."); } public String format(String moneyString){ int dotPoint = moneyString.indexOf("."); //判斷是否爲小數 String moneyStr; if(dotPoint != -1){ moneyStr = moneyString.substring(0,moneyString.indexOf(".")); } else{ moneyStr = moneyString; } StringBuffer fraction = null; //小數部分的處理,以及最後的yuan. StringBuffer ms = new StringBuffer(); for(int i = 0;i < moneyStr.length();i++){ ms.append(pattern[moneyStr.charAt(i) - 48]); //按數組的編號加入對應大寫漢字 } int cpCursor = 1; for(int j = moneyStr.length() - 1;j > 0;j--){ ms.insert(j,cPattern[cpCursor]); //在j以後加字符,不影響j對原字符串的相對位置 //只是moneyStr.length()不斷增長 //insert(j,"string")就在j位置處插入,j=0時爲第一位 cpCursor = cpCursor == 8?1:cpCursor + 1; //億位以後從新循環 } while(ms.indexOf("零拾") != -1){ //當十位爲零時用一個"零"代替"零拾" //replace的起始於終止位置 ms.replace(ms.indexOf("零拾"),ms.indexOf("零拾") + 2,ZEOR); } while(ms.indexOf("零佰") != -1){ //當百位爲零時,同理 ms.replace(ms.indexOf("零佰"),ms.indexOf("零佰") + 2,ZEOR); } while(ms.indexOf("零仟") != -1){ //同理 ms.replace(ms.indexOf("零仟"),ms.indexOf("零仟") + 2,ZEOR); } while(ms.indexOf("零萬") != -1){ //萬需保留,中文習慣 ms.replace(ms.indexOf("零萬"),ms.indexOf("零萬") + 2,"萬"); } while(ms.indexOf("零億") != -1){ //同上 ms.replace(ms.indexOf("零億"),ms.indexOf("零億") + 2,"億"); } while(ms.indexOf("零零") != -1){//有連續數位出現零,即有如下狀況,此時根據習慣保留一個零便可 ms.replace(ms.indexOf("零零"),ms.indexOf("零零") + 2,ZEOR); } while(ms.indexOf("億萬") != -1){ //特殊狀況,如:100000000,根據習慣保留高位 ms.replace(ms.indexOf("億萬"),ms.indexOf("億萬") + 2,"億"); } while(ms.lastIndexOf("零") == ms.length()-1){ //當結尾爲零j,沒必要顯示,通過處理也只可能出現一個零 if(ms.indexOf("零") == -1){ ms.delete(ms.lastIndexOf("零"),ms.lastIndexOf("零") + 1); }else{ break; } } int end; if((dotPoint = moneyString.indexOf(".")) != -1 ){ //是小數的進入 String fs = moneyString.substring(dotPoint + 1,moneyString.length()); if(fs.indexOf("00") == -1 || fs.indexOf("00") >= 2){//若前兩位小數全爲零,則跳過操做 end = fs.length() > 2?2:fs.length(); //僅保留兩位小數 fraction = new StringBuffer(fs.substring(0,end)); for(int j = 0;j < fraction.length();j++){ fraction.replace(j,j+1,this.pattern[fraction.charAt(j) - 48]); //替換大寫漢字 } for(int i = fraction.length();i > 0;i--){ //插入中文標識 fraction.insert(i,cfPattern[i]); } fraction.insert(0,"元"); //爲整數部分添加標識 } else{ fraction = new StringBuffer("元整"); } } else{ fraction = new StringBuffer("元整"); } ms.append(fraction); //加入小數部分 return ms.toString(); } public static void main(String [] ar){ System.out.println(new MoneyFormat().format("10005022.123009")); System.out.println(new MoneyFormat().format("0.12")); } }
上面是第一種方法,簡單些,容易理解,下面這種複雜點,兩個方法均可以實現這個功能 java
public class MoneyUtil { public static String[] chineseDigits = new String[] { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"}; /** * 把金額轉換爲漢字表示的數量,小數點後四捨五入保留兩位 * @param amount * @return */ public static String amountToChinese(double amount) { if(amount > 99999999999999.99 || amount < -99999999999999.99) throw new IllegalArgumentException("參數值超出容許範圍 (-99999999999999.99 ~ 99999999999999.99)!"); boolean negative = false; if(amount < 0) { negative = true; amount = amount * (-1); } long temp = Math.round(amount * 100); int numFen = (int)(temp % 10); // 分 temp = temp / 10; int numJiao = (int)(temp % 10); //角 temp = temp / 10; //temp 目前是金額的整數部分 int[] parts = new int[20]; // 其中的元素是把原來金額整數部分分割爲值在 0~9999 之間的數的各個部分 int numParts = 0; // 記錄把原來金額整數部分分割爲了幾個部分(每部分都在 0~9999 之間) for(int i=0; ; i++) { if(temp ==0) break; int part = (int)(temp % 10000); parts[i] = part; numParts ++; temp = temp / 10000; } boolean beforeWanIsZero = true; // 標誌「萬」下面一級是否是 0 String chineseStr = ""; for(int i=0; i<numParts; i++) { String partChinese = partTranslate(parts[i]); if(i % 2 == 0) { if("".equals(partChinese)) beforeWanIsZero = true; else beforeWanIsZero = false; } if(i != 0) { if(i % 2 == 0) chineseStr = "億" + chineseStr; else { if("".equals(partChinese) && !beforeWanIsZero) // 若是「萬」對應的 part 爲 0,而「萬」下面一級不爲 0,則不加「萬」,而加「零」 chineseStr = "零" + chineseStr; else { if(parts[i-1] < 1000 && parts[i-1] > 0) // 若是"萬"的部分不爲 0, 而"萬"前面的部分小於 1000 大於 0, 則萬後面應該跟「零」 chineseStr = "零" + chineseStr; chineseStr = "萬" + chineseStr; } } } chineseStr = partChinese + chineseStr; } if("".equals(chineseStr)) // 整數部分爲 0, 則表達爲"零元" chineseStr = chineseDigits[0]; else if(negative) // 整數部分不爲 0, 而且原金額爲負數 chineseStr = "負" + chineseStr; chineseStr = chineseStr + "元"; if(numFen == 0 && numJiao == 0) { chineseStr = chineseStr + "整"; } else if(numFen == 0) { // 0 分,角數不爲 0 chineseStr = chineseStr + chineseDigits[numJiao] + "角"; } else { // 「分」數不爲 0 if(numJiao == 0) chineseStr = chineseStr + "零" + chineseDigits[numFen] + "分"; else chineseStr = chineseStr + chineseDigits[numJiao] + "角" + chineseDigits[numFen] + "分"; } return chineseStr; } /** * 把一個 0~9999 之間的整數轉換爲漢字的字符串,若是是 0 則返回 "" * @param amountPart * @return */ private static String partTranslate(int amountPart) { if(amountPart < 0 || amountPart > 10000) { throw new IllegalArgumentException("參數必須是大於等於 0,小於 10000 的整數!"); } String[] units = new String[] {"", "拾", "佰", "仟"}; int temp = amountPart; String amountStr = new Integer(amountPart).toString(); int amountStrLength = amountStr.length(); boolean lastIsZero = true; //在從低位往高位循環時,記錄上一位數字是否是 0 String chineseStr = ""; for(int i=0; i<amountStrLength; i++) { if(temp == 0) // 高位已無數據 break; int digit = temp % 10; if(digit == 0) { // 取到的數字爲 0 if(!lastIsZero) //前一個數字不是 0,則在當前漢字串前加「零」字; chineseStr = "零" + chineseStr; lastIsZero = true; } else { // 取到的數字不是 0 chineseStr = chineseDigits[digit] + units[i] + chineseStr; lastIsZero = false; } temp = temp / 10; } return chineseStr; } public static void main(String[] args) { if(args.length == 0) { System.out.println("轉換演示:"); System.out.println("-------------------------"); System.out.println("0000000000000000001.01: " + amountToChinese(0000000000000000001.01)); System.out.println("45689263.626: " + amountToChinese(45689263.626)); System.out.println("0.69457: " + amountToChinese(0.69457)); System.out.println("253.0: " + amountToChinese(253.0)); System.out.println("0: " + amountToChinese(0)); System.out.println("-------------------------"); System.out.println("999: " + amountToChinese(999)); //System.out.println(Long.MAX_VALUE); //System.out.println(Long.MIN_VALUE); } else { System.out.println("轉換結果:"); System.out.println(args[0] + ": " + amountToChinese(Double.parseDouble(args[0]))); } } }