public class Test {
/*
* <p>Description: 字符串處理的公共類</p>
* <p>Copyright 2006 </p>
* @author leonards
* @Create Date : 2006-11-23
*/
/*
* 將字符串轉(zhuǎn)換成中文的大寫貨幣值
* @param moneyStr
* @return
*/
public static String convertToCapitalMoney(String moneyStr) {
double money = 0;
try {
money = Double.parseDouble(moneyStr);
} catch (Exception e) {
}
return convertToCapitalMoney(money);
}
/*
* 將數(shù)字轉(zhuǎn)換成中文的大寫貨幣值
* @param moneyValue
* @return
*/
public static String convertToCapitalMoney(double moneyValue) {
double money = moneyValue + 0.005; // 防止浮點(diǎn)數(shù)四舍五入造成誤差
String Result = "";
String capitalLetter = "零壹貳叁肆伍陸柒捌玖";
String moneytaryUnit = "分角圓拾佰仟萬(wàn)拾佰仟億拾佰仟萬(wàn)拾佰仟億拾佰仟";
String tempCapital, tempUnit;
int integer; // 錢的整數(shù)部分
int point; // 錢的小數(shù)部分
int tempValue; // 錢的每一位的值
integer = (int) money;
point = (int) (100 * (money - (float) integer));
if (integer == 0)
Result = "零圓";
/*
* 貨幣整數(shù)部分操作
* 1. 依次取得每一位上的值
* 2. 轉(zhuǎn)換成大寫
* 3. 確定貨幣單位
*/
for (int i = 1; integer > 0; i++) {
tempValue = (integer % 10);
tempCapital = capitalLetter.substring(tempValue, tempValue + 1);
tempUnit = moneytaryUnit.substring(i + 1, i + 2);
Result = tempCapital + tempUnit + Result;
integer = integer / 10;
}
/*
* 貨幣小數(shù)部分操作
*/
tempValue = (point / 10);
for (int i = 1; i > -1; i--) {
tempCapital = capitalLetter.substring(tempValue, tempValue + 1);
tempUnit = moneytaryUnit.substring(i, i + 1);
Result = Result + tempCapital + tempUnit;
tempValue = point % 10;
}
return Result;
}
public static void main(String[] args) {
String money1 = Test.convertToCapitalMoney("400000000.215");
System.out.println(money1);
String money = Test.convertToCapitalMoney(40000000.215);
System.out.println(money);
}
}