import java.text.DecimalFormat;
public class ChangeMoney{
//我写的.
public static String moneyToRMB(double d){
String[] digit = {
"无", "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰",
"仟"};
String[] capi = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
DecimalFormat df = new DecimalFormat("###0.00");
char[] c = df.format(d).toCharArray() ;
StringBuffer buf = new StringBuffer();
String reStr = null;
int cLength = c.length;
for(int i=0;i<c.length;i++){
String s = ""+c;
if(s.equals(".")){
cLength++;
continue;
}
int cp = Integer.parseInt(s);
buf.append(capi[ cp ]);
buf.append(digit[cLength-i-1]);
}
reStr = buf.toString();
System.out.println(":::::"+reStr);
for(int j=0;j<2;j++){
reStr = reStr.replaceAll( "零零", "零");//replaceAll:jdk1.4才有
reStr = reStr.replaceAll( "零亿", "亿");
reStr = reStr.replaceAll( "零万", "万");
reStr = reStr.replaceAll( "零仟", "零");
reStr = reStr.replaceAll( "零佰", "零");
reStr = reStr.replaceAll( "零拾", "零");
reStr = reStr.replaceAll( "零角", "零");
reStr = reStr.replaceAll( "亿万", "亿零");
reStr = reStr.replaceAll( "零分", "元整");
reStr = reStr.replaceAll( "零元", "元");
reStr = reStr.replaceAll( "零零", "零");
reStr = reStr.replaceAll( "角元整", "角整");
reStr = reStr.replaceAll( "元元", "元");
}
return reStr;
}
//吴兵写的
/**
* 将数据转换为金额大写
* @param amount – 待转换的字符串
* @return 被转换后的金额大写。
*/
public static String money2RMB(double amount) {
String returnStr = "";
String[] strFigure = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
double oldAmount = amount;
String[] strUnit = {
"无", "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰",
"仟"};
double tempAmount = amount;
amount = Math.abs(amount);
if (tempAmount != amount) {
returnStr = "负";
}
DecimalFormat df = new DecimalFormat("###0.00");
String strAmount = df.format(amount);
strAmount = replace(strAmount, ".", "");
int strLength = strAmount.length();
String myNum = "";
for (int i = 1; i <= strLength; i++) {
myNum = strAmount.substring(i – 1, i);
int len = strLength + 1 – i;
returnStr += strFigure[Integer.parseInt(myNum)] + strUnit[len];
}
//System.out.println(returnStr);
for (int i = 1; i < strUnit.length; i++) {
String tempStr = "";
if (! (strUnit.equals("万") || strUnit.equals("亿"))) {
tempStr = "零" + strUnit + "零";
returnStr = replace(returnStr, tempStr, "零");
}
}
returnStr = replace(returnStr, "零亿", "亿");
returnStr = replace(returnStr, "零万", "万");
returnStr = replace(returnStr, "零仟", "零");
returnStr = replace(returnStr, "零佰", "零");
returnStr = replace(returnStr, "零拾", "零");
returnStr = replace(returnStr, "零角", "零");
returnStr = replace(returnStr, "亿万", "亿零");
if (returnStr.endsWith("零分")) {
returnStr = replace(returnStr, "零分", "元整");
}
returnStr = replace(returnStr, "元元", "元");
return returnStr;
}
/**
* 将line中所有的oldString用newString替换.
*
* @param line 提供将要替换的字符串
* @param oldString 被替换掉的字符串
* @param newString 替换成的字符串
*
* @return 所有的oldString用newString替换的line
*/
public static final String replace(String line, String oldString,
String newString) {
if (line == null) {
return null;
}
int i = 0;
if ( (i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ( (i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i – j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length – j);
return buf.toString();
}
return line;
}
public static void main(String[] args){
System.out.println(money2RMB(70000050002.100));
System.out.println(moneyToRMB(70000050002.100));
}
};