把金額轉換成漢字大寫金額的Java代碼
class MoneyFormat{
private final String [] pattern ={"零","壹","貳","叁","肆","伍","陸","柒","捌","玖"};
private final String [] cPattern ={"","拾","佰","仟","萬","拾","佰","仟","億"};
private final String [] cfPattern = {"","角","分"};
private final String ZEOR = "零";
public MoneyFormat(){
System.out.println("run...");
}
public String format(String moneyString){
int dotPoint = moneyString.indexOf("."); //判斷是否為小數
String moneyStr;
if(dotPoint != -1){
moneyStr = moneyString.substring(0,moneyString.indexOf("."));
}
else{
moneyStr = moneyString;
}
StringBuffer fraction = null; //小數部分的處理,以及最后的yuan.
StringBuffer ms = new StringBuffer();
for(int i = 0;i < moneyStr.length();i++){
ms.append(pattern[moneyStr.charAt(i) - 48]); //按數組的編號加入對應大寫漢字
}
int cpCursor = 1;
for(int j = moneyStr.length() - 1;j > 0;j--){
ms.insert(j,cPattern[cpCursor]); //在j之后加字符,不影響j對原字符串的相對位置
//只是moneyStr.length()不斷增加
//insert(j,"string")就在j位置處插入,j=0時為第一位
cpCursor = cpCursor == 8?1:cpCursor + 1; //億位之后重新循環
}
while(ms.indexOf("零拾") != -1){ //當十位為零時用一個"零"代替"零拾"
//replace的起始于終止位置
ms.replace(ms.indexOf("零拾"),ms.indexOf("零拾") + 2,ZEOR);
}
while(ms.indexOf("零佰") != -1){ //當百位為零時,同理
ms.replace(ms.indexOf("零佰"),ms.indexOf("零佰") + 2,ZEOR);
}
while(ms.indexOf("零仟") != -1){ //同理
ms.replace(ms.indexOf("零仟"),ms.indexOf("零仟") + 2,ZEOR);
}
while(ms.indexOf("零萬") != -1){ //萬需保留,中文習慣
ms.replace(ms.indexOf("零萬"),ms.indexOf("零萬") + 2,"萬");
}
while(ms.indexOf("零億") != -1){ //同上
ms.replace(ms.indexOf("零億"),ms.indexOf("零億") + 2,"億");
}
while(ms.indexOf("零零") != -1){//有連續數位出現零,即有以下情況,此時根據習慣保留一個零即可
ms.replace(ms.indexOf("零零"),ms.indexOf("零零") + 2,ZEOR);
}
while(ms.indexOf("億萬") != -1){ //特殊情況,如:100000000,根據習慣保留高位
ms.replace(ms.indexOf("億萬"),ms.indexOf("億萬") + 2,"億");
}
while(ms.lastIndexOf("零") == ms.length()-1){ //當結尾為零j,不必顯示,經過處理也只可能出現一個零
if(ms.indexOf("零") == -1){
ms.delete(ms.lastIndexOf("零"),ms.lastIndexOf("零") + 1);
}else{
break;
}
}
int end;
if((dotPoint = moneyString.indexOf(".")) != -1 ){ //是小數的進入
String fs = moneyString.substring(dotPoint + 1,moneyString.length());
if(fs.indexOf("00") == -1 || fs.indexOf("00") >= 2){//若前兩位小數全為零,則跳過操作
end = fs.length() > 2?2:fs.length(); //僅保留兩位小數
fraction = new StringBuffer(fs.substring(0,end));
for(int j = 0;j < fraction.length();j++){
fraction.replace(j,j+1,this.pattern[fraction.charAt(j) - 48]); //替換大寫漢字
}
for(int i = fraction.length();i > 0;i--){ //插入中文標識
fraction.insert(i,cfPattern[i]);
}
fraction.insert(0,"元"); //為整數部分添加標識
}
else{
fraction = new StringBuffer("元整");
}
}
else{
fraction = new StringBuffer("元整");
}
ms.append(fraction); //加入小數部分
return ms.toString();
}
public static void main(String [] ar){
System.out.println(new MoneyFormat().format("10005022.123009"));
System.out.println(new MoneyFormat().format("0.12"));
}
} </pre> <p>上面是第一種方法,簡單些,容易理解,下面這種復雜點,兩個方法都可以實現這個功能 </p>
public class MoneyUtil {
public static String[] chineseDigits = new String[] { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖"};
/**
* 把金額轉換為漢字表示的數量,小數點后四舍五入保留兩位
* @param amount
* @return
*/
public static String amountToChinese(double amount) {
if(amount > 99999999999999.99 || amount < -99999999999999.99)
throw new IllegalArgumentException("參數值超出允許范圍 (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;
if(amount < 0) {
negative = true;
amount = amount * (-1);
}
long temp = Math.round(amount * 100);
int numFen = (int)(temp % 10); // 分
temp = temp / 10;
int numJiao = (int)(temp % 10); //角
temp = temp / 10;
//temp 目前是金額的整數部分
int[] parts = new int[20]; // 其中的元素是把原來金額整數部分分割為值在 0~9999 之間的數的各個部分
int numParts = 0; // 記錄把原來金額整數部分分割為了幾個部分(每部分都在 0~9999 之間)
for(int i=0; ; i++) {
if(temp ==0)
break;
int part = (int)(temp % 10000);
parts[i] = part;
numParts ++;
temp = temp / 10000;
}
boolean beforeWanIsZero = true; // 標志“萬”下面一級是不是 0
String chineseStr = "";
for(int i=0; i<numParts; i++) {
String partChinese = partTranslate(parts[i]);
if(i % 2 == 0) {
if("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if(i != 0) {
if(i % 2 == 0)
chineseStr = "億" + chineseStr;
else {
if("".equals(partChinese) && !beforeWanIsZero) // 如果“萬”對應的 part 為 0,而“萬”下面一級不為 0,則不加“萬”,而加“零”
chineseStr = "零" + chineseStr;
else {
if(parts[i-1] < 1000 && parts[i-1] > 0) // 如果"萬"的部分不為 0, 而"萬"前面的部分小于 1000 大于 0, 則萬后面應該跟“零”
chineseStr = "零" + chineseStr;
chineseStr = "萬" + chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if("".equals(chineseStr)) // 整數部分為 0, 則表達為"零元"
chineseStr = chineseDigits[0];
else if(negative) // 整數部分不為 0, 并且原金額為負數
chineseStr = "負" + chineseStr;
chineseStr = chineseStr + "元";
if(numFen == 0 && numJiao == 0) {
chineseStr = chineseStr + "整";
}
else if(numFen == 0) { // 0 分,角數不為 0
chineseStr = chineseStr + chineseDigits[numJiao] + "角";
}
else { // “分”數不為 0
if(numJiao == 0)
chineseStr = chineseStr + "零" + chineseDigits[numFen] + "分";
else
chineseStr = chineseStr + chineseDigits[numJiao] + "角" + chineseDigits[numFen] + "分";
}
return chineseStr;
}
/**
* 把一個 0~9999 之間的整數轉換為漢字的字符串,如果是 0 則返回 ""
* @param amountPart
* @return
*/
private static String partTranslate(int amountPart) {
if(amountPart < 0 || amountPart > 10000) {
throw new IllegalArgumentException("參數必須是大于等于 0,小于 10000 的整數!");
}
String[] units = new String[] {"", "拾", "佰", "仟"};
int temp = amountPart;
String amountStr = new Integer(amountPart).toString();
int amountStrLength = amountStr.length();
boolean lastIsZero = true; //在從低位往高位循環時,記錄上一位數字是不是 0
String chineseStr = "";
for(int i=0; i<amountStrLength; i++) {
if(temp == 0) // 高位已無數據
break;
int digit = temp % 10;
if(digit == 0) { // 取到的數字為 0
if(!lastIsZero) //前一個數字不是 0,則在當前漢字串前加“零”字;
chineseStr = "零" + chineseStr;
lastIsZero = true;
}
else { // 取到的數字不是 0
chineseStr = chineseDigits[digit] + units[i] + chineseStr;
lastIsZero = false;
}
temp = temp / 10;
}
return chineseStr;
}
public static void main(String[] args) {
if(args.length == 0) {
System.out.println("轉換演示:");
System.out.println("-------------------------");
System.out.println("0000000000000000001.01: " + amountToChinese(0000000000000000001.01));
System.out.println("45689263.626: " + amountToChinese(45689263.626));
System.out.println("0.69457: " + amountToChinese(0.69457));
System.out.println("253.0: " + amountToChinese(253.0));
System.out.println("0: " + amountToChinese(0));
System.out.println("-------------------------");
System.out.println("999: " + amountToChinese(999));
//System.out.println(Long.MAX_VALUE);
//System.out.println(Long.MIN_VALUE);
}
else {
System.out.println("轉換結果:");
System.out.println(args[0] + ": " + amountToChinese(Double.parseDouble(args[0])));
}
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!