早上一直在忙,没来及回复,留一个凑热闹吧 import java.math.*; import java.util.*; public class MoneyUtil { private static final String[] NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[][] NUMBER_UNIT = {{"", "拾", "佰", "仟"}, {"", "万", "亿", "兆"}}; private static final String[] MONEY_UNIT = {"元", "角", "分", "厘"};
public static String format(String str) throws Exception { if (str == null) {throw new Exception("parameter error.");} if (str.indexOf(",") > 0) { str = str.replaceAll(",", ""); } if (!str.matches("\\d*(.\\d*)?")) { throw new Exception("parameter format error."); }
if (str.indexOf(".") >= 0) { str = new BigDecimal(str).setScale(3, BigDecimal.ROUND_HALF_UP).toString(); }
String num, dec; int idx = str.indexOf("."); if (idx >= 0) { num = str.substring(0, idx); dec = str.substring(idx+1); if (num.isEmpty()) {num = "0";} } else { num = str; dec = "0"; } if (num.length() > (int)Math.pow(4, NUMBER_UNIT[1].length)) { throw new Exception("overflow."); }
StringBuilder buf = new StringBuilder(); char[] c = num.toCharArray(); for (int i=0, j=(c.length-1)%4, u=(c.length-1)/4; i<c.length; i++, j--) { if (j<0) { j = NUMBER_UNIT[0].length-1; } if (c[i]-'0' > 0) { buf.append(NUMBER[c[i]-'0']).append(NUMBER_UNIT[0][j]); } else if (i != 0 && i != c.length-1) { buf.append(NUMBER[0]); } if ((c.length-1-i)%4 == 0) { buf.append(NUMBER_UNIT[1][u]); u--; } } if (buf.length() > 0) {buf.append(MONEY_UNIT[0]);}
c = dec.toCharArray(); for (int i=0; i<Math.min(c.length, MONEY_UNIT.length-1); i++) { if (c[i]-'0' > 0) { buf.append(NUMBER[c[i]-'0']).append(MONEY_UNIT[i+1]); } }
return buf.toString(); }
public static String format(double money) throws Exception { return format(String.valueOf(money)); } public static void main(String[] args) throws Exception { //String money = "000,123,456,789.123456789"; //System.out.println(format(money)); Scanner scanner = new Scanner(System.in); while (true) { System.out.print("please intput a double string or [quit] to exit:"); String buf = scanner.nextLine(); if ("quit".equals(buf)) {break;} if (buf.isEmpty()) {continue;} try { System.out.printf("%s -> %s\n", buf, format(buf)); } catch (Exception e) { e.printStackTrace(); } } } }
http://cjjwzs.iteye.com/blog/1001934
public static String CN_MoneyValueOf(String moneyValue) {
String value = moneyValue.replaceFirst("^0+", "");
value = value.replaceAll(",", "");
int dot_pos = value.indexOf('.');
String int_value;
String fraction_value;
if (dot_pos == -1) {
int_value = value;
fraction_value = "00";
} else {
int_value = value.substring(0, dot_pos);
fraction_value = value.substring(dot_pos + 1, value.length())
+ "00".substring(0, 2);
} int len = int_value.length();
if (len>16) return "0";
StringBuffer cn_currency = new StringBuffer();
String[] CN_Carry = new String[] { "", "万", "亿", "万" };
int cnt = len/4+(len%4==0?0:1);
int partLen = len-(cnt-1)*4;
String partValue=null;
boolean bZero=false;
String curCN=null;
for(int i =0; i<cnt; i++){
partValue = int_value.substring(0,partLen);
int_value=int_value.substring(partLen);
curCN = Part2CN(partValue,i!=0&&!"零".equals(curCN));
if(bZero && !"零".equals(curCN)){
cn_currency.append("零");
bZero=false;
}
if("零".equals(curCN))bZero=true;
if(!"零".equals(curCN)){
cn_currency.append(curCN);
cn_currency.append(CN_Carry[cnt-1-i]);
}
partLen=4;
partValue=null;
}
cn_currency.append("元"); int fv1 = Integer.parseInt(fraction_value.substring(0,1));
int fv2 = Integer.parseInt(fraction_value.substring(1,2));
if(fv1+fv2==0){
cn_currency.append("整");
}
else{
cn_currency.append(CN_Digits[fv1]).append("角");
cn_currency.append(CN_Digits[fv2]).append("分");
}
return cn_currency.toString();
}
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.replace(",", "")));
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;
}
} public static void main(String[] args) {
String number = "1.23";
System.out.println(number + " " + ChineseMoneyUtil.toChinese(number));
number = "1234567890123456.123";
System.out.println(number + " " + ChineseMoneyUtil.toChinese(number));
number = "0.0798";
System.out.println(number + " " + ChineseMoneyUtil.toChinese(number));
number = "10,001,000.09";
System.out.println(number + " " + ChineseMoneyUtil.toChinese(number));
number = "01.107700";
System.out.println(number + " " + ChineseMoneyUtil.toChinese(number));
}}
import java.math.*;
import java.util.*;
public class MoneyUtil {
private static final String[] NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[][] NUMBER_UNIT = {{"", "拾", "佰", "仟"}, {"", "万", "亿", "兆"}};
private static final String[] MONEY_UNIT = {"元", "角", "分", "厘"};
public static String format(String str) throws Exception {
if (str == null) {throw new Exception("parameter error.");}
if (str.indexOf(",") > 0) {
str = str.replaceAll(",", "");
}
if (!str.matches("\\d*(.\\d*)?")) {
throw new Exception("parameter format error.");
}
if (str.indexOf(".") >= 0) {
str = new BigDecimal(str).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
}
String num, dec;
int idx = str.indexOf(".");
if (idx >= 0) {
num = str.substring(0, idx);
dec = str.substring(idx+1);
if (num.isEmpty()) {num = "0";}
} else {
num = str;
dec = "0";
}
if (num.length() > (int)Math.pow(4, NUMBER_UNIT[1].length)) {
throw new Exception("overflow.");
}
StringBuilder buf = new StringBuilder();
char[] c = num.toCharArray();
for (int i=0, j=(c.length-1)%4, u=(c.length-1)/4; i<c.length; i++, j--) {
if (j<0) {
j = NUMBER_UNIT[0].length-1;
}
if (c[i]-'0' > 0) {
buf.append(NUMBER[c[i]-'0']).append(NUMBER_UNIT[0][j]);
} else if (i != 0 && i != c.length-1) {
buf.append(NUMBER[0]);
}
if ((c.length-1-i)%4 == 0) {
buf.append(NUMBER_UNIT[1][u]);
u--;
}
}
if (buf.length() > 0) {buf.append(MONEY_UNIT[0]);}
c = dec.toCharArray();
for (int i=0; i<Math.min(c.length, MONEY_UNIT.length-1); i++) {
if (c[i]-'0' > 0) {
buf.append(NUMBER[c[i]-'0']).append(MONEY_UNIT[i+1]);
}
}
return buf.toString();
}
public static String format(double money) throws Exception {
return format(String.valueOf(money));
} public static void main(String[] args) throws Exception {
//String money = "000,123,456,789.123456789";
//System.out.println(format(money));
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("please intput a double string or [quit] to exit:");
String buf = scanner.nextLine();
if ("quit".equals(buf)) {break;}
if (buf.isEmpty()) {continue;}
try {
System.out.printf("%s -> %s\n", buf, format(buf));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}