java數值型貨幣的大寫轉換

package util;
import java.text.*;
import java.util.Locale;

public class ZHTNumberFormatter {
private static String pointPostfixNum[]={
    "圓","角","分","釐"
};

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

private static String bigger[]={
    "",
    "萬",
    "億",
    "兆",
};
private static String pos[]={
    "",
    "拾",
    "佰",
    "仟"
};

public static String convertCurrency(double num){
   DecimalFormat format=new DecimalFormat("####,###0.00");
   String result="";
   String temp=format.format(num);
   String topParts[]=temp.split("//.");
   String pointPostfix=topParts[1];
   String parts[]=(topParts[0]).split("//,");

   int k=0;
   for(int i=parts.length;i>0;i--){
    if(Integer.parseInt(parts[i-1])>0)
     result=subConvert(parts[i-1])+bigger[k]+result;
    else
     result="零"+result;
    k++;
   }
   String newString=""+result.charAt(0);
   for(int i=1;i<result.length();i++){
    if(result.charAt(i)=='零'&&result.charAt(i-1)=='零')
     continue;
    newString+=result.charAt(i);
   }
   if(newString.charAt(newString.length()-1)=='零')
    newString=newString.substring(0,newString.length()-1);
  
   newString=newString+"圓";
   temp="";
   if(Integer.parseInt(pointPostfix)!=0){
    if(pointPostfix.charAt(1)!='0')
     temp=number[pointPostfix.charAt(1)-'0']+"分";
    if(pointPostfix.charAt(0)!='0')
     temp=number[pointPostfix.charAt(0)-'0']+"角"+temp;
    else if(!temp.equals(""))
     temp="零"+temp;
    newString=newString+temp;
   }else{
    newString=newString+"整";
   }
   return "人民幣"+newString;  
}
private static String subConvert(String part){
   String result="";
   int posIndex=0;
   for(int max=Math.min(4,part.length());
    max>0;
    max--)
   {
    if(part.charAt(max-1)!='0')
     result=number[part.charAt(max-1)-'0']+pos[posIndex]+result;
    else
     result="零"+result;
    posIndex++;
   }
   String newString=""+result.charAt(0);
   for(int i=1;i<result.length();i++){
    if(result.charAt(i)=='零'&&result.charAt(i-1)=='零')
     continue;
    newString+=result.charAt(i);
   }
   if(newString.charAt(newString.length()-1)=='零')
    newString=newString.substring(0,newString.length()-1);  
   return newString;
  
}

public static void main(String args[]){
   System.out.println(convertCurrency(100100111000.010001d));
  
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章