java實現,中文財務讀取金額方式

這是我的第一篇文章。

使用java實現的,中文財務讀取金額方式。如:12345讀作“壹萬貳仟叄拾肆元整”。


一般拿到一個數字,首先看是否存在小數,然後按照整數部分和小數部分分別讀取,然後組合成最終的結果。

實現過程已經參考了財務讀取金額的教程,如有錯誤,歡迎指正。


下面是源代碼:

/**
 * 目前支持千萬億級別兩位小數的讀取
 * @author Renqiang_cheng
 *
 */
public class ReadMoney {


private static final String[] NUM_READ = { "零", "壹", "貳", "叄", "肆", "伍",
"陸", "柒", "捌", "玖" };//讀取的數字


private static final String[] UNIT_READ = { "", "拾", "佰", "仟", "萬", "拾",
"佰", "仟", "億", "拾", "佰", "仟", "萬" };//各個數位


private static final String[] CLASS_READ = { "元", "角", "分" };//單位


private static final String[] SPECIAL_READ = { "整" };//特殊字符


public static void main(String[] args) {
System.out.println(getReadInChinese("1234567891234"));
System.out.println(getReadInChinese("1234567891234.00"));
System.out.println(getReadInChinese("891234.05"));
System.out.println(getReadInChinese("891234.58"));
System.out.println(getReadInChinese("891234.80"));
System.out.println(getReadInChinese("891230.58"));
System.out.println(getReadInChinese("891200.58"));
System.out.println(getReadInChinese("890000.58"));
System.out.println(getReadInChinese("800000.58"));

System.out.println(getReadInChinese("1200007000034"));

}


/**
* 讀取整數部分
* @param integer 整數字符串,如:12345
* @return
*/
public static String getReadInChineseForInteger(String integer) {
StringBuilder b = new StringBuilder();
int len = integer.length();
int index = len - 1;
for (int i = 0; i < len; i++) {
int numIndex = Integer.valueOf(integer.substring(i, i + 1));
b.append(NUM_READ[numIndex]);
if (numIndex != 0 || index == 8 || index == 4) {
b.append(UNIT_READ[index]);
}
index--;
}
String result = b.toString();
// 消零操作
if (hasDoubleZero(result)) {
result = replaceDoubleZeroAsZero(result);
}
if (endWithZero(result)) {
result = replaceEndWithZeroAsUnit(result);
}
return result;
}


public static String replaceEndWithZeroAsUnit(String result) {
if (result.indexOf("零億") > -1) {
result = result.replaceFirst("零億", "億");
}
if (result.indexOf("零萬") > -1) {
result = result.replaceFirst("零萬", "萬");
}
if (result.endsWith("零")) {
result = result.substring(0, result.length() - 1);
}
if (endWithZero(result)) {
result = replaceEndWithZeroAsUnit(result);
}
return result;
}


public static String replaceDoubleZeroAsZero(String result) {
result = result.replaceAll("零零", "零");
if (hasDoubleZero(result)) {
result = replaceDoubleZeroAsZero(result);
}
return result;
}


public static boolean hasDoubleZero(String r) {
return r.indexOf("零零") > -1;
}


public static boolean endWithZero(String r) {
return r.indexOf("零億") > -1 || r.indexOf("零萬") > -1 || r.endsWith("零");
}


/**
* 讀取小數部分
* @param decimal 小數字符串,不包括小數點,如:58 05 80 00 等
* @return
*/
public static String getReadInChineseForDecimal(String decimal) {
StringBuilder b = new StringBuilder();
int len = decimal.length();
if(len > 0){
int numIndex = Integer.valueOf(decimal.substring(0, 1));
b.append(NUM_READ[numIndex]);
if (numIndex != 0) {
b.append(CLASS_READ[1]);
}
}
if(len > 1){
int numIndex = Integer.valueOf(decimal.substring(1, 2));
b.append(NUM_READ[numIndex]);
if (numIndex != 0) {
b.append(CLASS_READ[2]);
}
}
String result = b.toString();
// 消零操作
if (hasDoubleZero(result)) {
result = replaceDoubleZeroAsZero(result);
}
if (endWithZero(result)) {
result = replaceEndWithZeroAsUnit(result);
}
return result;
}


/**
* 使用財務讀取字符串
* @param s 千萬億級別的兩位小數字符串
* @return
*/
public static String getReadInChinese(String s) {
int pointIndex = s.indexOf(".");
String integer = "";
String decimal = "";
if (pointIndex > -1) {
String[] nums = s.split("\\.");
integer += getReadInChineseForInteger(nums[0]);
decimal += getReadInChineseForDecimal(nums[1]);
} else {
integer = getReadInChineseForInteger(s);
}
StringBuilder b = new StringBuilder(integer);
b.append(CLASS_READ[0]);
if(decimal.equals("")){
// 財務方面,如果以元結尾要寫SPECIAL_READ
b.append(SPECIAL_READ[0]);
}else{
b.append(decimal);
}
return b.toString();
}
}

發佈了33 篇原創文章 · 獲贊 12 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章