记录下一些常用的函数,以便下次使用
/**
* 格式化金额
* @param s
* @param len
* @return
*/
public static String formatMoney(String s, int len)
{
if (s == null || s.length() < 1) {
return "";
}
NumberFormat formater = null;
double num = Double.parseDouble(s);
if (len == 0) {
formater = new DecimalFormat("###,###");
} else {
StringBuffer buff = new StringBuffer();
buff.append("###,###.");
for (int i = 0; i < len; i++) {
buff.append("#");
}
formater = new DecimalFormat(buff.toString());
}
String result = formater.format(num);
if(result.indexOf(".") == -1)
{
result = "¥" + result + ".00";
}
else
{
result = "¥" + result;
}
return result;
}
调用该方法
formatMoney("-87094753316.74", 2)
输出格式分:
¥-87,094,753,316.74
将阿拉伯数字转换成对应的大写,不是很完善:
public static String getUpperChineseMoney(String amount) {
if (null==amount||"".equals(amount)||amount.indexOf("-")!=-1)
return "";
if (amount == null||Double.parseDouble(amount)==0) {
return "零元整";
}
boolean islt0 = false;
if(Double.parseDouble(amount) < 0.001){
islt0=true;
amount = (-Double.parseDouble(amount))+"";
}
final String[] upperUnit = {
"万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万",
"仟", "佰", "拾", "元"};
final String[] upperNumber = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒",
"捌", "玖"};
final String[] upperDec = {
"角", "分"};
StringBuffer convertedMoney = new StringBuffer("");
String returnString = "";
try {
System.out.println(amount+"----------------------");
DecimalFormat dcf = new DecimalFormat("###########0.00");
String numString = dcf.format(Double.parseDouble(amount));
System.out.println(numString+"----------------------");
numString=numString.replaceAll(",",".");
System.out.println(numString+"----------------------");
int dotPos = numString.indexOf(".");
String integerStr = numString.substring(0, dotPos);
String decStr = numString.substring(dotPos + 1);
int lenInt = integerStr.length();
int lenDec = 0;
if ("00".equalsIgnoreCase(decStr)) {
lenDec = 0;
} else {
lenDec = 2;
}
if (lenInt > 13) {
return "数字太大";
}
boolean zeroFlg = false;
int lenEmpty = 13 - lenInt;
for (int i = 0; i < lenInt; i++) {
String number = integerStr.substring(i, i + 1);
String unit = upperUnit[lenEmpty + i];
String upNum = upperNumber[Integer.parseInt(number)];
if ("0".equalsIgnoreCase(number)) {
if (!zeroFlg) {
zeroFlg = true;
}
if ("亿".equals(unit) || "万".equals(unit) || "元".equals(unit)) {
convertedMoney.append(unit);
}
} else {
if (zeroFlg) {
convertedMoney.append("零").append(upNum).append(unit);
zeroFlg = false;
} else {
convertedMoney.append(upNum).append(unit);
}
}
}
if (lenDec == 0) {
convertedMoney.append("整");
} else {
String jiao = upperNumber[Integer.parseInt(decStr.substring(0,
1))];
if (!"0".equals(decStr.substring(0, 1))) {
convertedMoney.append(jiao).append(upperDec[0]);
} else {
convertedMoney.append(jiao);
}
String fen = upperNumber[Integer.parseInt(decStr.substring(1, 2))];
if (!"0".equals(decStr.substring(1, 2))) {
convertedMoney.append(fen).append(upperDec[1]);
} else {
convertedMoney.append("整");
}
}
returnString = convertedMoney.toString();
if (judgeUnit(returnString)) {
returnString = returnString.replaceFirst("亿万", "亿");
}
} catch (Exception ex) {
ex.printStackTrace();
return "输入格式不正确!";
}
if(islt0){
returnString="负"+returnString;
}
return returnString;
}
调用该方法
getUpperChineseMoney("54545454");
输出格式分:
伍仟肆佰伍拾肆万伍仟肆佰伍拾肆元整