package fank.j2se;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class MoneyUtils {
/** 大写数字 */
private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍",
"陆", "柒", "捌", "玖" };
/** 整数部分的单位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰",
"仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
/** 小数部分的单位 */
private static final String[] DUNIT = { "角", "分", "厘" };
/**
* 得到大写金额。
*/
public static String toChinese(String str) {
str = str.replaceAll(",", "");// 去掉","
String integerStr;// 整数部分数字
String decimalStr;// 小数部分数字
// 初始化:分离整数部分和小数部分
if (str.indexOf(".") > 0) {
integerStr = str.substring(0, str.indexOf("."));
decimalStr = str.substring(str.indexOf(".") + 1);
} else if (str.indexOf(".") == 0) {
integerStr = "";
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = "";
}
// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
}
// overflow超出处理能力,直接返回
if (integerStr.length() > IUNIT.length) {
System.out.println(str + ":超出处理能力");
return str;
}
int[] integers = toArray(integerStr);// 整数部分数字
boolean isMust5 = isMust5(integerStr);// 设置万单位
int[] decimals = toArray(decimalStr);// 小数部分数字
return getChineseInteger(integers, isMust5)
+ getChineseDecimal(decimals);
}
/**
* 整数部分和小数部分转换为数组,从高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}
/**
* 得到中文金额的整数部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i < length; i++) {
// 0出现在关键位置:1234(万)5678(亿)9012(万)3456(元)
// 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// 万(亿)(必填)
key = IUNIT[4];
else if ((length - i) == 9)// 亿(必填)
key = IUNIT[8];
else if ((length - i) == 5 && isMust5)// 万(不必填)
key = IUNIT[4];
else if ((length - i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0时补零,不包含最后一位
if ((length - i) > 1 && integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key
: (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
}
return chineseInteger.toString();
}
/**
* 得到中文金额的小数部分。
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i < decimals.length; i++) {
// 舍去3位小数之后的
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? ""
: (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}
/**
* 判断第5位数字的单位"万"是否应加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) {
// 取得从低位数,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}
/**
* 获取格式化的人民币(四舍五入)
*
* @param money
* 待处理的人民币
* @param scale
* 小数点后保留的位数
* @param divisor
* 格式化值(万,百万,亿等等)
* @return
*/
public static String getFormatMoney(double money, int scale, double divisor) {
if (divisor == 0) {
return "0.00";
}
if (scale < 0) {
return "0.00";
}
BigDecimal moneyBD = new BigDecimal(money);
BigDecimal divisorBD = new BigDecimal(divisor);
// RoundingMode.HALF_UP = 2
return moneyBD.divide(divisorBD, scale, RoundingMode.HALF_UP)
.toString();
}
/**
* 获取会计格式的人民币(四舍五入)——添加会计标识:',' 例如:int scale = 2; double divisor = 10000.00;
* double money = 1269486459.86; accountantMoney =
* MoneyUtils.getAccountantMoney(money, scale, divisor); 结果:126,948.65万元
*
* @param money
* 待处理的人民币
* @param scale
* 小数点后保留的位数
* @param divisor
* 格式化值(万,百万,亿等等)
* @return
*/
public static String getAccountantMoney(double money, int scale,
double divisor) {
String disposeMoneyStr = getFormatMoney(money, scale, divisor);
// 小数点处理
int dotPosition = disposeMoneyStr.indexOf(".");
String exceptDotMoeny = null;// 小数点之前的字符串
String dotMeony = null;// 小数点之后的字符串
if (dotPosition > 0) {
exceptDotMoeny = disposeMoneyStr.substring(0, dotPosition);
dotMeony = disposeMoneyStr.substring(dotPosition);
} else {
exceptDotMoeny = disposeMoneyStr;
}
// 负数处理
int negativePosition = exceptDotMoeny.indexOf("-");
if (negativePosition == 0) {
exceptDotMoeny = exceptDotMoeny.substring(1);
}
StringBuffer reverseExceptDotMoney = new StringBuffer(exceptDotMoeny);
reverseExceptDotMoney.reverse();// 字符串倒转
// reverse(reverseExceptDotMoeny);
char[] moneyChar = reverseExceptDotMoney.toString().toCharArray();
StringBuffer returnMeony = new StringBuffer();// 返回值
for (int i = 0; i < moneyChar.length; i++) {
if (i != 0 && i % 3 == 0) {
returnMeony.append(",");// 每隔3位加','
}
returnMeony.append(moneyChar[i]);
}
returnMeony.reverse();// 字符串倒转
// reverse(returnMeony);
if (dotPosition > 0) {
returnMeony.append(dotMeony);
}
if (negativePosition == 0) {
return "-" + returnMeony.toString();
} else {
return returnMeony.toString();
}
}
/**
* 字符串倒转方法
*
* @author chanson
* @param oldStr
*/
// public void reverse(StringBuffer oldStr){
// char[] oldStrChar = oldStr.toString().toCharArray();
// StringBuffer newStr = new StringBuffer();
// for(int i = oldStrChar.length - 1; i > -1; i--){
// newStr.append(oldStrChar[i]);
// }
// oldStr = newStr;
// }
public static void main(String[] args) {
double money = -1269486459.86;
int scale = 2;
double divisor = 10000.00;
System.out.println("原货币值: " + money);
// System.out.println("货币值: "+util.getAccountantMoney(money, scale,
// 1));
String formatMeony = MoneyUtils.getFormatMoney(money, scale, divisor);
System.out.println("格式化货币值: " + formatMeony + "万元");
String accountantMoney = MoneyUtils.getAccountantMoney(money, scale,
divisor);
System.out.println("会计货币值: " + accountantMoney + "万元");
System.out.println(MoneyUtils.toChinese("4566230000123.23"));
}
}