Java工具類---金額轉換工具 MoneyUtil
package com.luang.util.common;import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
- MoneyUtil.java *
- @desc Money轉換工具
- @author Guoxp
@datatime Apr 7, 2013 3:47:51 PM / public class MoneyUtil {
private static final Pattern AMOUNT_PATTERN =Pattern.compile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$"); // 不考慮分隔符的正確性
private static final char[] RMB_NUMS = "零壹貳叁肆伍陸柒捌玖".toCharArray();
private static final String[] UNITS = {"元", "角", "分", "整"};
private static final String[] U1 = {"", "拾", "佰", "仟"};
private static final String[] U2 = {"", "萬", "億"};/**
- 將金額(整數部分等于或少于12位,小數部分2位)轉換為中文大寫形式.
- @param amount 金額數字
- @return 中文大寫
@throws IllegalArgumentException
*/
public static String convert(String amount) throws IllegalArgumentException {
// 去掉分隔符
amount = amount.replace(",", "");// 驗證金額正確性
if (amount.equals("0.00")) {
throw new IllegalArgumentException("金額不能為零.");
}
Matcher matcher = AMOUNT_PATTERN.matcher(amount);
if (! matcher.find()) {
throw new IllegalArgumentException("輸入金額有誤.");
}String integer = matcher.group(1); // 整數部分
String fraction = matcher.group(2); // 小數部分String result = "";
if (! integer.equals("0")) {
result += integer2rmb(integer) + UNITS[0]; // 整數部分
}
if (fraction.equals("00")) {
result += UNITS[3]; // 添加[整]
} else if (fraction.startsWith("0") && integer.equals("0")) {
result += fraction2rmb(fraction).substring(1); // 去掉分前面的[零]
} else {
result += fraction2rmb(fraction); // 小數部分
}return result;
}// 將金額小數部分轉換為中文大寫
private static String fraction2rmb(String fraction) {
char jiao = fraction.charAt(0); // 角
char fen = fraction.charAt(1); // 分
return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : ""))+ (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : "");
}
// 將金額整數部分轉換為中文大寫
private static String integer2rmb(String integer) {
StringBuilder buffer = new StringBuilder();
// 從個位數開始轉換
int i, j;
for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {
char n = integer.charAt(i);
if (n == '0') {// 當n是0且n的右邊一位不是0時,插入[零] if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') { buffer.append(RMB_NUMS[0]); } // 插入[萬]或者[億] if (j % 4 == 0) { if (i > 0 && integer.charAt(i - 1) != '0' || i > 1 && integer.charAt(i - 2) != '0' || i > 2 && integer.charAt(i - 3) != '0') { buffer.append(U2[j / 4]); } }
} else {
if (j % 4 == 0) { buffer.append(U2[j / 4]); // 插入[萬]或者[億] } buffer.append(U1[j % 4]); // 插入[拾]、[佰]或[仟] buffer.append(RMB_NUMS[n - '0']); // 插入數字
}
}
return buffer.reverse().toString();
}
/**- 對金額的格式調整到分
- @param money
- @return
*/
public static String moneyFormat(String money){//23->23.00
StringBuffer sb=new StringBuffer();
if(money==null){
}return "0.00";
int index=money.indexOf(".");
if(index==-1){
}else{return money+".00";
}String s0=money.substring(0,index);//整數部分 String s1=money.substring(index+1);//小數部分 if(s1.length()==1){//小數點后一位 s1=s1+"0"; }else if(s1.length()>2){//如果超過3位小數,截取2位就可以了 s1=s1.substring(0,2); } sb.append(s0); sb.append("."); sb.append(s1);
return sb.toString();
}
/** - @param args
*/
public static void main(String[] args) {
System.out.println(MoneyUtil.moneyFormat("123.2803"));
System.out.println(MoneyUtil.convert("1000.00"));
}
} </pre>來自:http://blog.csdn.net/guoxuepeng123/article/details/8797923