/**
* 金額小數位數格式化
*
* @param type 過濾方式2:四捨五入2位小數;3:四捨五入3位小數;4:四捨五入4位小數;14:截位法4位小數;5:四捨五入5位小數;6:四捨五入6位小數;8:四捨五入8位小數;9:四捨五入9位小數;19:截位法9位小數;25:四捨五入15位小數;35:截位法15位小數
* @param data 需要進行格式化的數據
* @return
*/
public List<Object> moneyDataFilter(Double data, Integer type) {
// 過濾方式df
String[] arr = { "2", "3", "4", "14", "5", "6", "8", "9", "19", "25", "35" };
DecimalFormat df = null;// 執行公式
String formate = "#,###,##0.";// 表達式
if (type != null && Arrays.asList(arr).contains("" + type+ "")) {// 爲正確數值
type = type == 25 ? type - 10 : type;
type = type == 14 ? type - 10 : type;
type = type == 19 ? type - 10 : type;
type = type == 35 ? type - 20 : type;
for (int i = 0; i < type; i++) {
formate += "0";
}
df = new DecimalFormat(formate);
if (Arrays.asList(new String[] { "2", "3", "4", "5", "6", "8", "9", "25" }).contains("" + type + "")) {// 爲四捨五入
df.setRoundingMode(RoundingMode.HALF_UP);
} else {// 不進行四捨五入
df.setRoundingMode(RoundingMode.FLOOR);
}
data != null ? df.format(data) : df.format(0));
}
return data;
}
/**
* 大寫金額
* @param numberOfMoney 需轉換金額
* @return
*/
public static String getUpperChineseMoney(BigDecimal numberOfMoney) {
String[] CN_UPPER_NUMBER = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒",
"捌", "玖" };
String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "萬",
"拾", "佰", "仟", "億", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
String CN_FULL = "";
String CN_NEGATIVE = "負";
int MONEY_PRECISION = 2;
String CN_ZEOR_FULL = "零元" + CN_FULL;
StringBuffer sb = new StringBuffer();
int signum = numberOfMoney.signum();
// 零元整的情況
if (signum == 0) {
return CN_ZEOR_FULL;
}
// 這裏會進行金額的四捨五入
long number = numberOfMoney.movePointRight(MONEY_PRECISION)
.setScale(0, 4).abs().longValue();
// 得到小數點後兩位值
long scale = number % 100;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
// 判斷最後兩位數,一共有四中情況:00 = 0, 01 = 1, 10, 11
if (!(scale > 0)) {
numIndex = 2;
number = number / 100;
getZero = true;
}
if ((scale > 0) && (!(scale % 10 > 0))) {
numIndex = 1;
number = number / 10;
getZero = true;
}
int zeroSize = 0;
while (true) {
if (number <= 0) {
break;
}
// 每次獲取到最後一個數
numUnit = (int) (number % 10);
if (numUnit > 0) {
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
} else {
++zeroSize;
if (!(getZero)) {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
if (numIndex == 2) {
if (number > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
getZero = true;
}
// 讓number每次都去掉最後一個數
number = number / 10;
++numIndex;
}
// 如果signum == -1,則說明輸入的數字爲負數,就在最前面追加特殊字符:負
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
// 輸入的數字小數點後兩位爲"00"的情況,則要在最後追加特殊字符:整
if (!(scale > 0)) {
sb.append(CN_FULL);
}
return sb.toString();
}