java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

阿拉伯数字和中文大写的相互转换

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56629
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArabToChinese {
public Stack<String>stack;
public ArabToChinese(){
stack
=new Stack<String>();
}
   
public void pushToStack(Stack<String>stack,char c){
       
switch(c){
       
case '0':
            stack.push(
"");
           
break;
       
case '1':
            stack.push(
"");
           
break;
       
case '2':
            stack.push(
"");
           
break;
       
case '3':
            stack.push(
"");
           
break;
       
case '4':
            stack.push(
"");
           
break;
       
case '5':
            stack.push(
"");
           
break;
       
case '6':
            stack.push(
"");
           
break;
       
case '7':
            stack.push(
"");
           
break;
       
case '8':
            stack.push(
"");
           
break;
       
case '9':
            stack.push(
"");
           
break;   
        }   
    }
   
public void pushToStack_(Stack<String>stack,String s){
        
int digit1=5;
        
int digit2=9;
        
int digit3=1;
       
for(int i=s.length()-1,j=0,k=0;i>=0;i--){
            
char c=s.charAt(i);
             j
++;
             k
++;
            
if(k==2){ stack.push("");
             }
            
else if(k==3){
                 stack.push(
"");
             }
            
else if(k==4){
                 stack.push(
"");
                 k
=0;
             }
            
if(j==digit1){
                 stack.push(
"");
                 digit1
+=8;
             }
            
if(j==digit2){
                 stack.push(
"亿");
                 digit2
+=8;
             }
           
if(j==digit3&&c=='0') {
                digit3
+=4;
               
continue;
            }
           
else pushToStack(stack,c);
         }
    }
   
   
public String popToString(Stack<String>stack){
        String s
="";
        StringBuffer buffer
=new StringBuffer();
       
while(stack.empty()==false){
            buffer.append((String)stack.pop());
        }
        s
=buffer.toString();
        s
=s.replaceAll("零仟","");
        s
=s.replaceAll("零佰","");
        s
=s.replaceAll("零拾","");
        s
=s.replaceAll("[零]+","");
        s
=s.replaceAll("零萬","");
        s
=s.replaceAll("零亿","亿");
        s
=s.replace("零角","");
        s
=s.replace("零分","");
       
return s;
    }
   
public static void main(String args[]) throws IOException{
    
boolean available=false;    
     String s
="";
     String s1
="";
     System.out.print(
"请输入一个数字:");
    
while(!available){
     BufferedReader buffer;
     buffer
=new BufferedReader(new InputStreamReader(System.in));
     s
=buffer.readLine();
     Pattern pattern
=Pattern.compile("^[^0][0-9]+(.[0-9]{1,2})$");
     Matcher matcher
=pattern.matcher(s);
    
boolean isMatched=matcher.find();
    
if(isMatched==true){
          available
=true;
     }
    
else System.out.println("你输入的数字有错误,请重新输入:");
}
     ArabToChinese atc
=new ArabToChinese();
    
int index=s.indexOf('.');
    
if(index!=-1){
        s1
=s.substring(index+1);
        s
=s.substring(0,index);
        atc.pushToStack_(atc.stack, s);
        s
=atc.popToString(atc.stack)+"";
       
if(s1.length()==1){
            s1
=s1+"0";
        }
     
for(int i=1,j=0;i>=0;i--){
         
char c=s1.charAt(i);
          j
++;
         
if(j==1){
              atc.stack.push(
"");
          }
         
if(j==2){
              atc.stack.push(
"");
          }
          atc.pushToStack(atc.stack,c);
      }
     
     
      s1
=atc.popToString(atc.stack);
      System.out.println(
"转换后的金钱数字为:"+s+s1);
}
else {atc.pushToStack_(atc.stack,s);
       s
=atc.popToString(atc.stack)+"";
       s
=s.replace("零圆","");
       System.out.println(
"转换后的金钱数字为:"+s);
   }
  }
}


java code
            
public class ChineseToArabDigit { public static void main(String args[]){ long num_Buffer=0; long num_add=0; long num_mutiply=0; long result_yi=0; long result_wan=0; char c1=''; String s="壹亿亿圆"; // String s="叁仟柒佰贰拾壹萬贰仟叁佰柒拾壹亿捌仟贰佰柒拾叁萬壹仟贰佰捌拾壹圆"; int index=s.indexOf(''); s=s.substring(0,index); for(int i=0;i<s.length();i++){ char c=s.charAt(i); int index_lastyi=s.lastIndexOf('亿'); if(i!=s.length()-1){ c1=s.charAt(i+1); } switch(c){ case '': num_Buffer=0; break; case '': num_Buffer=1; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=1; } break; case '': num_Buffer=2; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=2; } break; case '': num_Buffer=3; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=3; } break; case '': num_Buffer=4; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=4; } break; case '': num_Buffer=5; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=5; } break; case '': num_Buffer=6; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=6; } break; case '': num_Buffer=7; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=7; } break; case '': num_Buffer=8; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=8; } break; case '': num_Buffer=9; if(c1==''||c1=='亿'||i==s.length()-1){ num_add+=9; } break; /**************************************/ case '': num_add+=10*num_Buffer; break; case '': num_add+=100*num_Buffer; break; case '' : num_add+=1000*num_Buffer; break; /****************************************/ case '': if(index_lastyi!=-1&&i<index_lastyi){ num_mutiply=num_add*10000; } else if(index_lastyi==-1||i>index_lastyi){ result_wan=num_add*10000; } num_add=0; break; case '亿': num_mutiply=(num_mutiply+num_add)*100000000; result_yi=num_mutiply; num_add=0; break; } } System.out.println(result_yi+result_wan+num_add); } }

posted on 2009-06-10 21:53 rrong_m 阅读(507) 评论(0)  编辑  收藏

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜