RMB轉換大寫

public class RMB{git


private final static String[] STR_NUMBER = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖" };app

private final static String[] STR_UNIT = { "", "拾", "佰", "仟", "萬", "拾","佰", "仟", "億", "拾", "佰", "仟" };// 整數單位spa

private final static String[] STR_UNIT2 = { "角", "分", "釐" };// 小數單位orm

public static void main(String[] args) {ci

// TODO Auto-generated method stubget

Scanner scan= new Scanner(System.in);input

System.out.println("Please input a number: ");string

String convert=convert(scan.nextDouble());it

System.out.println(convert);ast

}

public static boolean IsNumber(String str){

char[] c=str.toCharArray();

for(int i=0;i<c.length;i++){

if(Character.isDigit(c[i])){

}else{

return false;

}

}

return true;

}

public static String convert(double d){

DecimalFormat df=new DecimalFormat("#0.###");

String strNum=df.format(d);

if(strNum.indexOf(".")!=-1){

String num=strNum.substring(0, strNum.indexOf("."));

if(num.length()>12){

System.out.println("number is too big,can not be convert!");

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;

}

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();

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;

}

public static String getDecimal(String num){

if (num.indexOf(".") == -1){

return "";

}

num = num.substring(num.indexOf(".") + 1);

num = new StringBuffer(num).reverse().toString();

StringBuffer temp = new StringBuffer();

for (int i = 0; i < num.length(); i++){

temp.append(STR_NUMBER[num.charAt(i) - 48]);

temp.append(STR_UNIT2[i]);

}

num = temp.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;

}

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;

}


}

相關文章
相關標籤/搜索