java數字轉中文大寫貨幣思路
『壹』 用java編譯金額的中文大寫轉換。
/**
* 金額小數轉換成中文大寫金額
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "萬", "千", "佰", "拾", "億", "千", "佰",
"拾", "萬", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "貳", "叄", "肆", "伍", "陸",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 將金額小數轉換成中文大寫金額
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money < 0 || money > MAX_VALUE)
return "參數非法!";
long money1 = Math.round(money * 100); // 四捨五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用於選擇金額數值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用於選擇金額單位
boolean isZero = false; // 用於判斷當前為是否為零
String result = "";
for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "億" || UNIT[unitIndex] == "萬"
|| UNIT[unitIndex] == "元") { // 如果當前位是億、萬、元,且數值為零
result = result + UNIT[unitIndex]; //補單位億、萬、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分結尾就加"整"字
if (!result.endsWith("角")&&!result.endsWith("分")) {
result = result + "整";
}
//例如沒有這行代碼,數值"400000001101.2",輸出就是"肆千億萬壹千壹佰零壹元貳角"
result = result.replaceAll("億萬", "億");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您輸入的金額(小寫)為:" + value);
System.out.println("您輸入的金額(大寫)為:" + convertMoney(value));
}
}
『貳』 JAVA 數字轉成中文的思路只要思路,代碼不需要 謝謝!
沒有小數點吧首先做個2映射 1對一,2對二等等還有一個1對十,2對百,等等等這個映射,其實就是Map的Key和value的關系如果是整數的話,循環對10取余,1234%10=4,然後根據第一個映射,4映射四放到字元串里,做個標記變數,記錄循環次數,1對十,2對百,再加到字元串里 代碼如下import java.util.HashMap;
import java.util.Map;public class Tsss {
public static void main(String[] args) {
int m=2354745;
int i=0;
Tsss t=new Tsss();
String s="";
while(m!=0){
if(i!=0){
s=map1.get(i)+s;
}
int n=m%10;
System.out.println(n);
s=map.get(n)+s;
m=m/10;
i++;
}
System.out.println(s);
}
public static Map<Integer,String> map=new HashMap<Integer,String>();
{
map.put(1, "一");
map.put(2, "二");
map.put(3, "三");
map.put(4, "四");
map.put(5, "五");
map.put(6, "六");
map.put(7, "七");
map.put(8, "八");
map.put(9, "九");
map.put(0, "十");
}
public static Map<Integer,String> map1=new HashMap<Integer,String>();
{
map1.put(1, "十");
map1.put(2, "百");
map1.put(3, "千");
map1.put(4, "萬");
map1.put(5, "十萬");
map1.put(6, "百萬");
map1.put(7, "千萬");
map1.put(8, "億");
map1.put(9, "十億");
map1.put(0, "百億");
}
}
『叄』 Java中怎麼將小寫阿拉伯數字轉換成中文大寫,不是金額的轉行,而是直接轉換成大寫漢字。
public class Admin {
public static void main(String[] args) {
String[] arr = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖" };
String str = "123456";
char[] c = str.toCharArray();
for (int i = 0; i < c.length; i++) {
int a = Integer.parseInt(String.valueOf(c[i]));
System.out.print(arr[a]);
}
}
}
『肆』 Java 關於中文大寫金額與阿拉伯數字 互相轉換的問題(eclipse版)
eclipse中用java實現中文和阿拉伯數字互轉的方法如下:
import java.io.*;
import java.lang.IllegalArgumentException;
public class ConvertNum{
/**
* 把金額阿拉伯數字轉換為漢字表示,小數點後四捨五入保留兩位
* 還有一種方法可以在轉換的過程中不考慮連續0的情況,然後對最終的結果進行一次遍歷合並連續的零
*/
public static String [] ChineseNum = new String[]{"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
public static String NumToChinese(double num){
if(num > 99999999999999.99 || num < -99999999999999.99)
throw new IllegalArgumentException("參數值超出允許范圍 (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;//正負標號
if(num<0){
negative = true;
num = num*(-1);
}
long temp = Math.round(num*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;//記錄把原來金額整數部分分割為幾個部分
for(int i=0;;i++){
if(temp == 0)
break;
int part = (int)(temp%10000);
parts[i] =part;
temp = temp/10000;
numParts++;
}
boolean beforeWanIsZero = true;//標志位,記錄萬的下一級是否為0
String chineseStr = "";
for(int i=0;i<numParts;i++){
String partChinese = partConvert(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 = ChineseNum[0];
else if(negative)//整數部分部位0,但是為負數
chineseStr = "負" +chineseStr;
chineseStr = chineseStr + "元";
if(numFen==0&&numJiao==0){
chineseStr = chineseStr +"整";
}
else if(numFen==0){//0分
chineseStr = chineseStr +ChineseNum[numJiao] + "角";
}
else{
if(numJiao==0)
chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
else
chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分";
}
return chineseStr;
}
//轉換拆分後的每個部分,0-9999之間
public static String partConvert(int partNum){
if(partNum<0||partNum>10000){
throw new IllegalArgumentException("參數必須是大於等於0或小於10000的整數");
}
String [] units = new String[]{"","拾","佰","仟"};
int temp = partNum;
String partResult = new Integer(partNum).toString();
int partResultLength = partResult.length();
boolean lastIsZero = true;//記錄上一位是否為0
String chineseStr = "";
for(int i=0;i<partResultLength;i++){
if(temp == 0)//高位無數字
break;
int digit = temp%10;
if(digit == 0){
if(!lastIsZero)//如果前一個數字不是0則在當前漢字串前加零
chineseStr = "零"+chineseStr;
lastIsZero = true;
}
else{
chineseStr = ChineseNum[digit] + units[i] +chineseStr;
lastIsZero = false;
}
temp =temp/10;
}
return chineseStr;
}
public static void main(String args []){
double num = 0;
System.out.println("請輸入金額數字,-1退出");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
while(num!=-1){
System.out.println(num+NumToChinese(num));
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
}
System.out.println("其他測試:");
System.out.println("100120: " + NumToChinese(100120));
System.out.println("25000000000005.999: " + NumToChinese(25000000000005.999));
System.out.println("45689263.626: " + NumToChinese(45689263.626));
System.out.println("0.69457: " + NumToChinese(0.69457));
System.out.println("253.0: " + NumToChinese(253.0));
System.out.println("0: " + NumToChinese(0));
}
}
『伍』 Java金額的中文大寫方式
/**
* 金額小數轉換成中文大寫金額
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "萬", "千", "佰", "拾", "億", "千", "佰",
"拾", "萬", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "貳", "叄", "肆", "伍", "陸",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 將金額小數轉換成中文大寫金額
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money < 0 || money > MAX_VALUE)
return "參數非法!";
long money1 = Math.round(money * 100); // 四捨五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用於選擇金額數值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用於選擇金額單位
boolean isZero = false; // 用於判斷當前為是否為零
String result = "";
for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "億" || UNIT[unitIndex] == "萬"
|| UNIT[unitIndex] == "元") { // 如果當前位是億、萬、元,且數值為零
result = result + UNIT[unitIndex]; //補單位億、萬、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分結尾就加"整"字
if (!result.endsWith("角")&&!result.endsWith("分")) {
result = result + "整";
}
//例如沒有這行代碼,數值"400000001101.2",輸出就是"肆千億萬壹千壹佰零壹元貳角"
result = result.replaceAll("億萬", "億");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您輸入的金額(小寫)為:" + value);
System.out.println("您輸入的金額(大寫)為:" + convertMoney(value));
}
}
『陸』 誰有簡化的方法用JAVA編寫一個把表示人民幣的阿拉伯數字轉化為大寫漢字的方法
public class RenMingBi {
/**
* @param args add by zxx ,Nov 29, 2008
*/
private static final char[] data = new char[]{
'零','壹','貳','叄','肆','伍','陸','柒','捌','玖'
};
private static final char[] units = new char[]{
'元','拾','佰','仟','萬','拾','佰','仟','億'
};
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(
convert(135689123));
}
public static String convert(int money)
{
StringBuffer sbf = new StringBuffer();
int unit = 0;
while(money!=0)
{
sbf.insert(0,units[unit++]);
int number = money%10;
sbf.insert(0, data[number]);
money /= 10;
}
return sbf.toString();
}
}
『柒』 java方式實現數字轉換成中文大寫
數組小標的問題吧,好好檢查一下,下標,最好使用debug
一步一步的看,到底是那一步出現問題。
我也有現成的代碼,如果實在不行,我可以給你一份。
希望 採納。
『捌』 JAVA 數字轉換為中文大寫的轉換
package com.heyang;
/**
* 將10億以內的阿拉伯數字轉成漢字大寫形式
* @author xizhenyin
*
*/
public class CnUpperCaser {
// 整數部分
private String integerPart;
// 小數部分
private String floatPart;
// 將數字轉化為漢字的數組,因為各個實例都要使用所以設為靜態
private static final char[] cnNumbers={'零','壹','貳','叄','肆','伍','陸','柒','捌','玖'};
// 供分級轉化的數組,因為各個實例都要使用所以設為靜態
private static final char[] series={'元','拾','百','仟','萬','拾','百','仟','億'};
/**
* 構造函數,通過它將阿拉伯數字形式的字元串傳入
* @param original
*/
public CnUpperCaser(String original){
// 成員變數初始化
integerPart="";
floatPart="";
if(original.contains(".")){
// 如果包含小數點
int dotIndex=original.indexOf(".");
integerPart=original.substring(0,dotIndex);
floatPart=original.substring(dotIndex+1);
}
else{
// 不包含小數點
integerPart=original;
}
}
/**
* 取得大寫形式的字元串
* @return
*/
public String getCnString(){
// 因為是累加所以用StringBuffer
StringBuffer sb=new StringBuffer();
// 整數部分處理
for(int i=0;i<integerPart.length();i++){
int number=getNumber(integerPart.charAt(i));
sb.append(cnNumbers[number]);
sb.append(series[integerPart.length()-1-i]);
}
// 小數部分處理
if(floatPart.length()>0){
sb.append("點");
for(int i=0;i<floatPart.length();i++){
int number=getNumber(floatPart.charAt(i));
sb.append(cnNumbers[number]);
}
}
// 返回拼接好的字元串
return sb.toString();
}
/**
* 將字元形式的數字轉化為整形數字
* 因為所有實例都要用到所以用靜態修飾
* @param c
* @return
*/
private static int getNumber(char c){
String str=String.valueOf(c);
return Integer.parseInt(str);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new CnUpperCaser("123456789.12345").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
System.out.println(new CnUpperCaser(".123456789").getCnString());
System.out.println(new CnUpperCaser("0.1234").getCnString());
System.out.println(new CnUpperCaser("1").getCnString());
System.out.println(new CnUpperCaser("12").getCnString());
System.out.println(new CnUpperCaser("123").getCnString());
System.out.println(new CnUpperCaser("1234").getCnString());
System.out.println(new CnUpperCaser("12345").getCnString());
System.out.println(new CnUpperCaser("123456").getCnString());
System.out.println(new CnUpperCaser("1234567").getCnString());
System.out.println(new CnUpperCaser("12345678").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
}
}
『玖』 怎樣用Java將金額轉換為中文大寫形式
String words="12334";
for(int i=0 ; i<words.length ; i++)
{
switch(words.charAt(i))
{
case "1":System.out.println("壹");break;
case "2":
..............
case "0":System.out.println("零");break;
}
}
大致思路是這個樣子的吧。
『拾』 JAVA 編程 輸出金額的中文大寫形式
public class Text {
/**
* 測試程序的可行性
* @param args
*/
public static void main(String[] args) {
System.out.println("--------將數字轉換成中文金額的大寫形式------------\n");
Trans2RMB t2r = new Trans2RMB();
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
// 如果轉換過後是一個空串,則不輸出屏幕
if(!"".equals(s)) {
System.out.println("轉換成中文後為:" + s);;
}
System.out.println("\n---------------------------------------------");
}
/**
* 從命令行接收一個數,在其中調用 checkNum() 方法對其進行
* 驗證,並返回相應的值
* @return 如果輸入合法,返回輸入的這個數
*/
private String getNum() {
String s = null;
System.out.println("請輸入一個數字(精確到小數點後兩位):");
// 從命令行輸入這個浮點數
java.util.Scanner scanner = new java.util.Scanner(System.in);
s = scanner.next();
// 判斷用戶輸入是否合法
// 若合法,返回這個值;若非法返回 "0"
if(this.checkNum(s)) {
return s;
} else {
return "";
}
}
/**
* 判斷用戶輸入的數據是否合法,用戶只能輸入大於零的數字,不能輸入其它字元
* @param s String
* @return 如果用戶輸入數據合法,返回 true,否則返回 false
*/
private boolean checkNum(String s) {
// 如果用戶輸入的數里有非數字字元,則視為非法數據,返回 false
try {
float f = Float.valueOf(s);
// 如果這個數小於零則視為非法數據,返回 false
if(f < 0) {
System.out.println("非法數據,請檢查!");
return false;
}else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("非法數據,請檢查!");
return false;
}
}
/**
* 把用戶輸入的數以小數點為界分割開來,並調用 numFormat() 方法
* 進行相應的中文金額大寫形式的轉換
* 註:傳入的這個數應該是經過 roundString() 方法進行了四捨五入操作的
* @param s String
* @return 轉換好的中文金額大寫形式的字元串
*/
private String splitNum(String s) {
// 如果傳入的是空串則繼續返回空串
if("".equals(s)) {
return "";
}
// 以小數點為界分割這個字元串
int index = s.indexOf(".");
// 截取並轉換這個數的整數部分
String intOnly = s.substring(0, index);
String part1 = this.numFormat(1, intOnly);
// 截取並轉換這個數的小數部分
String smallOnly = s.substring(index + 1);
String part2 = this.numFormat(2, smallOnly);
// 把轉換好了的整數部分和小數部分重新拼湊一個新的字元串
String newS = part1 + part2;
return newS;
}
/**
* 對傳入的數進行四捨五入操作
* @param s String 從命令行輸入的那個數
* @return 四捨五入後的新值
*/
private String roundString(String s) {
// 如果傳入的是空串則繼續返回空串
if("".equals(s)) {
return "";
}
// 將這個數轉換成 double 類型,並對其進行四捨五入操作
double d = Double.parseDouble(s);
// 此操作作用在小數點後兩位上
d = (d * 100 + 0.5) / 100;
// 將 d 進行格式化
s = new java.text.DecimalFormat("##0.000").format(d);
// 以小數點為界分割這個字元串
int index = s.indexOf(".");
// 這個數的整數部分
String intOnly = s.substring(0, index);
// 規定數值的最大長度只能到萬億單位,否則返回 "0"
if(intOnly.length()13) {
System.out.println("輸入數據過大!(整數部分最多13位!)");
return "";
}
// 這個數的小數部分
String smallOnly = s.substring(index + 1);
// 如果小數部分大於兩位,只截取小數點後兩位
if(smallOnly.length()2) {
String roundSmall = smallOnly.substring(0, 2);
// 把整數部分和新截取的小數部分重新拼湊這個字元串
s = intOnly + "." + roundSmall;
}
return s;
}
/**
* 把傳入的數轉換為中文金額大寫形式
* @param flag int 標志位,1 表示轉換整數部分,0 表示轉換小數部分
* @param s String 要轉換的字元串
* @return 轉換好的帶單位的中文金額大寫形式
*/
private String numFormat(int flag, String s) {
int sLength = s.length();
// 貨幣大寫形式
String bigLetter args) {
System.out.println("--------將數字轉換成中文金額的大寫形式------------\n");
Trans2RMB t2r = new Trans2RMB();
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
// 如果轉換過後是一個空串,則不輸出屏幕
if(!"".equals(s)) {
System.out.println("轉換成中文後為:" + s);;
}
System.out.println("\n---------------------------------------------");
}
/**
* 從命令行接收一個數,在其中調用 checkNum() 方法對其進行
* 驗證,並返回相應的值
* @return 如果輸入合法,返回輸入的這個數
*/
private String getNum() {
String s = null;
System.out.println("請輸入一個數字(精確到小數點後兩位):");
// 從命令行輸入這個浮點數
java.util.Scanner scanner = new java.util.Scanner(System.in);
s = scanner.next();
// 判斷用戶輸入是否合法
// 若合法,返回這個值;若非法返回 "0"
if(this.checkNum(s)) {
return s;
} else {
return "";
}
}
/**
* 判斷用戶輸入的數據是否合法,用戶只能輸入大於零的數字,不能輸入其它字元
* @param s String
* @return 如果用戶輸入數據合法,返回 true,否則返回 false
*/
private boolean checkNum(String s) {
// 如果用戶輸入的數里有非數字字元,則視為非法數據,返回 false
try {
float f = Float.valueOf(s);
// 如果這個數小於零則視為非法數據,返回 false
if(f < 0) {
System.out.println("非法數據,請檢查!");
return false;
}else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("非法數據,請檢查!");
return false;
}
}
/**
* 把用戶輸入的數以小數點為界分割開來,並調用 numFormat() 方法
* 進行相應的中文金額大寫形式的轉換
* 註:傳入的這個數應該是經過 roundString() 方法進行了四捨五入操作的
* @param s String
* @return 轉換好的中文金額大寫形式的字元串
*/
private String splitNum(String s) {
// 如果傳入的是空串則繼續返回空串
if("".equals(s)) {
return "";
}
// 以小數點為界分割這個字元串
int index = s.indexOf(".");
// 截取並轉換這個數的整數部分
String intOnly = s.substring(0, index);
String part1 = this.numFormat(1, intOnly);
// 截取並轉換這個數的小數部分
String smallOnly = s.substring(index + 1);
String part2 = this.numFormat(2, smallOnly);
// 把轉換好了的整數部分和小數部分重新拼湊一個新的字元串
String newS = part1 + part2;
return newS;
}
/**
* 對傳入的數進行四捨五入操作
* @param s String 從命令行輸入的那個數
* @return 四捨五入後的新值
*/
private String roundString(String s) {
// 如果傳入的是空串則繼續返回空串
if("".equals(s)) {
return "";
}
// 將這個數轉換成 double 類型,並對其進行四捨五入操作
double d = Double.parseDouble(s);
// 此操作作用在小數點後兩位上
d = (d * 100 + 0.5) / 100;
// 將 d 進行格式化
s = new java.text.DecimalFormat("##0.000").format(d);
// 以小數點為界分割這個字元串
int index = s.indexOf(".");
// 這個數的整數部分
String intOnly = s.substring(0, index);
// 規定數值的最大長度只能到萬億單位,否則返回 "0"
if(intOnly.length()13) {
System.out.println("輸入數據過大!(整數部分最多13位!)");
return "";
}
// 這個數的小數部分
String smallOnly = s.substring(index + 1);
// 如果小數部分大於兩位,只截取小數點後兩位
if(smallOnly.length()2) {
String roundSmall = smallOnly.substring(0, 2);
// 把整數部分和新截取的小數部分重新拼湊這個字元串
s = intOnly + "." + roundSmall;
}
return s;
}
/**
* 把傳入的數轉換為中文金額大寫形式
* @param flag int 標志位,1 表示轉換整數部分,0 表示轉換小數部分
* @param s String 要轉換的字元串
* @return 轉換好的帶單位的中文金額大寫形式
*/
private String numFormat(int flag, String s) {
int sLength = s.length();
// 貨幣大寫形式
String bigLetter[] = {"零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"};
// 貨幣單位
String unit[] = {"元", "拾", "佰", "仟", "萬",
// 拾萬位到仟萬位
"拾", "佰", "仟",
// 億位到萬億位
"億", "拾", "佰", "仟", "萬"};
String small[] = {"分", "角"};
// 用來存放轉換後的新字元串
String newS = "";
// 逐位替換為中文大寫形式
for(int i = 0; i < sLength; i ++) {
if(flag == 1) {
// 轉換整數部分為中文大寫形式(帶單位)
newS = newS + bigLetter[s.charAt(i) - 48] + unit[sLength - i - 1];
} else if(flag == 2) {
// 轉換小數部分(帶單位)
newS = newS + bigLetter[s.charAt(i) - 48] + small[sLength - i - 1];
}
}
return newS;
}
/**
* 把已經轉換好的中文金額大寫形式加以改進,清理這個字
* 符串裡面多餘的零,讓這個字元串變得更加可觀
* 註:傳入的這個數應該是經過 splitNum() 方法進行處理,這個字
* 符串應該已經是用中文金額大寫形式表示的
* @param s String 已經轉換好的字元串
* @return 改進後的字元串
*/
private String cleanZero(String s) {
// 如果傳入的是空串則繼續返回空串
if("".equals(s)) {
return "";
}
// 如果用戶開始輸入了很多 0 去掉字元串前面多餘的『零『,使其看上去更符合習慣
while(s.charAt(0) == 『零『) {
// 將字元串中的 "零" 和它對應的單位去掉
s = s.substring(2);
// 如果用戶當初輸入的時候只輸入了 0,則只返回一個 "零"
if(s.length() == 0) {
return "零";
}
}
// 字元串中存在多個『零『在一起的時候只讀出一個『零『,並省略多餘的單位
/* 由於本人對演算法的研究太菜了,只能用4個正則表達式去轉換了,各位大蝦別介意哈... */
String regex1[] = {"零仟", "零佰", "零拾"};
String regex2[] = {"零億", "零萬", "零元"};
String regex3[] = {"億", "萬", "元"};
String regex4[] = {"零角", "零分"};
// 第一輪轉換把 "零仟", 零佰","零拾"等字元串替換成一個"零"
for(int i = 0; i < 3; i ++) {
s = s.replaceAll(regex1[i], "零");
}
// 第二輪轉換考慮 "零億","零萬","零元"等情況
// "億","萬","元"這些單位有些情況是不能省的,需要保留下來
for(int i = 0; i < 3; i ++) {
// 當第一輪轉換過後有可能有很多個零疊在一起
// 要把很多個重復的零變成一個零
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
s = s.replaceAll(regex2[i], regex3[i]);
}
// 第三輪轉換把"零角","零分"字元串省略
for(int i = 0; i < 2; i ++) {
s = s.replaceAll(regex4[i], "");
}
// 當"萬"到"億"之間全部是"零"的時候,忽略"億萬"單位,只保留一個"億"
s = s.replaceAll("億萬", "億");
return s;
}
}