1. 程式人生 > >Java實現漢字金額轉化(轉自微信)

Java實現漢字金額轉化(轉自微信)

ack temp string 額的 rep ger poi 輸出 nec

package com.xfonlineclass;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* 金額轉換
*
* 小峰在線
* http://www.xfonlineclass.com
*/
public class ConvertMoney {
// 大寫數字
private final static String[] STR_NUMBER = { "零", "壹", "貳", "叁", "肆", "伍",
"陸", "柒", "捌", "玖" };
private final static String[] STR_UNIT = { "", "拾", "佰", "仟", "萬", "拾",
"佰", "仟", "億", "拾", "佰", "仟" };// 整數單位
private final static String[] STR_UNIT2 = { "角", "分", "厘" };// 小數單位

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);// 創建掃描器
System.out.println("請輸入一個金額");
// 獲取金額轉換後的字符串
String convert = convert(scan.nextDouble());
System.out.println(convert);// 輸出轉換結果
}

/**
* 獲取可數部分
*
* @param num
* 金額
* @return 金額整數部分
*/
public static String getInteger(String num) {
if (num.indexOf(".") != -1) { // 判斷是否包含小數點
num = num.substring(0, num.indexOf("."));
}
num = new StringBuffer(num).reverse().toString(); // 反轉字符串
StringBuffer temp = new StringBuffer(); // 創建一個StringBuffer對象
for (int i = 0; i < num.length(); i++) {// 加入單位
temp.append(STR_UNIT[i]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
}
num = temp.reverse().toString();// 反轉字符串
num = numReplace(num, "零拾", "零"); // 替換字符串的字符
num = numReplace(num, "零佰", "零"); // 替換字符串的字符
num = numReplace(num, "零仟", "零"); // 替換字符串的字符
num = numReplace(num, "零萬", "萬"); // 替換字符串的字符
num = numReplace(num, "零億", "億"); // 替換字符串的字符
num = numReplace(num, "零零", "零"); // 替換字符串的字符
num = numReplace(num, "億萬", "億"); // 替換字符串的字符
// 如果字符串以零結尾將其除去
if (num.lastIndexOf("零") == num.length() - 1) {
num = num.substring(0, num.length() - 1);
}
return num;
}

/**
* 獲取小數部分
*
* @param num
* 金額
* @return 金額的小數部分
*/
public static String getDecimal(String num) {
// 判斷是否包含小數點
if (num.indexOf(".") == -1) {
return "";
}
num = num.substring(num.indexOf(".") + 1);
// 反轉字符串
num = new StringBuffer(num).reverse().toString();
// 創建一個StringBuffer對象
StringBuffer temp = new StringBuffer();
// 加入單位
for (int i = 0; i < num.length(); i++) {
temp.append(STR_UNIT2[i]);
temp.append(STR_NUMBER[num.charAt(i) - 48]);
}
num = temp.reverse().toString(); // 替換字符串的字符
num = numReplace(num, "零角", "零"); // 替換字符串的字符
num = numReplace(num, "零分", "零"); // 替換字符串的字符
num = numReplace(num, "零厘", "零"); // 替換字符串的字符
num = numReplace(num, "零零", "零"); // 替換字符串的字符
// 如果字符串以零結尾將其除去
if (num.lastIndexOf("零") == num.length() - 1) {
num = num.substring(0, num.length() - 1);
}
return num;
}

/**
* 替換字符串中內容
*
* @param num
* 字符串
* @param oldStr
* 被替換內容
* @param newStr
* 新內容
* @return 替換後的字符串
*/
public static String numReplace(String num, String oldStr, String newStr) {
while (true) {
// 判斷字符串中是否包含指定字符
if (num.indexOf(oldStr) == -1) {
break;
}
// 替換字符串
num = num.replaceAll(oldStr, newStr);
}
// 返回替換後的字符串
return num;
}

/**
* 金額轉換
*
* @param d
* 金額
* @return 轉換成大寫的全額
*/
public static String convert(double d) {
// 實例化DecimalFormat對象
DecimalFormat df = new DecimalFormat("#0.###");
// 格式化double數字
String strNum = df.format(d);
// 判斷是否包含小數點
if (strNum.indexOf(".") != -1) {
String num = strNum.substring(0, strNum.indexOf("."));
// 整數部分大於12不能轉換
if (num.length() > 12) {
System.out.println("數字太大,不能完成轉換!");
return "";
}
}
String point = "";// 小數點
if (strNum.indexOf(".") != -1) {
point = "元";
} else {
point = "元整";
}
// 轉換結果
String result = getInteger(strNum) + point + getDecimal(strNum);
if (result.startsWith("元")) { // 判斷是字符串是否已"元"結尾
result = result.substring(1, result.length()); // 截取字符串
}
return result; // 返回新的字符串
}
}

  

Java實現漢字金額轉化(轉自微信)