Thinking in Mobile

学海无涯乐作舟

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 26 Stories :: 1 Comments :: 0 Trackbacks
支持千兆的小写金额转中文大写金额

import java.io.*;

public class Money 
{
    
static final String[] digits = {"","","","","","","","","",""};
    
static final String[] units = {"""""""""""""""亿"""""""""""""""""};
    
    
public static String display(String money){
        
if (money==null || money.trim().length()==0)
        
{
            
return "零元整";
        }

        money 
= money.trim();
        
if (money.matches("0"|| money.matches("\\."))
        
{
            
return "零元整";
        }

        StringBuffer sub 
= new StringBuffer("");
        
int dotPos = 0;
        String head 
= "";
        String tail 
= "";

        dotPos 
= money.lastIndexOf(".");
        
if (dotPos==-1)
        
{
            head 
= money;
            tail 
= "00";
        }

        
else {
            head 
= money.substring(0,dotPos);
            tail 
= money.substring(dotPos+1);
        }

        
if (tail.length()==1)
        
{
            tail 
+="0";
        }

        sub.append(parseHead(head));
        sub.append(parseTail(tail));
        
        
return sub.toString();
    }


    
static String parseHead(String head) {
        System.out.println(
"head = ["+head+"]");
        
if (head.length()==0 || head.equals("0"))
        
{
            
return "";
        }

        StringBuffer sub 
= new StringBuffer("");

        
if (head.length()==0)
        
{
            
return head;
        }

        
if (head.length()>units.length)
        
{
            
throw new RuntimeException("String too long. supported max size: "+units.length+", actual size: "+head.length());
        }

        
int uIndex = 0;//单位的起始索引
        if (head.length()!=units.length)
        
{
            uIndex 
= units.length-head.length();
        }

        
int zero = 0//'0'标志
        for (int i=0;i<head.length() ;i++ )
        
{
            
int d = Integer.parseInt(String.valueOf(head.charAt(i)));
            
//处理'0'
            if (d==0)
            
{
                
int pos = head.length()-i;
                
switch (pos)
                
{
                
case 13:
                    
if (zero!=3) sub.append("");
                    
if (zero!=0) zero=0;
                    
break;
                
case 9:
                    
if (zero!=3) sub.append("亿");
                    
if (zero!=0) zero=0;
                    
break;
                
case 5:
                    
if (zero!=3) sub.append("");
                    
if (zero!=0) zero=0;
                    
break;
                
default:
                    zero
++;
                }

                
if (i==head.length()-1)
                
{
                    sub.append(
"");
                }

                
continue;
            }

            
if (zero>0{
                sub.append(digits[
0]);
                zero
=0;
            }

            sub.append(digits[d]);
            sub.append(units[uIndex
+i]);
        }

        
return sub.toString();
    }


    
static String parseTail(String tail) {
        StringBuffer sub 
= new StringBuffer("");
        
if (tail==null)
        
{
            
throw new RuntimeException("Unsupported argument null");
        }

        
if (tail.length()==0 || tail.equals("00"))
        
{
            
return "";
        }

        
int d = Character.digit(tail.charAt(0),10);
        sub.append(digits[d]);
        
if (d!=0)
        
{
            sub.append(
"");
        }

        sub.append(digits[Character.digit(tail.charAt(
1),10)]).append("");
        
return sub.toString();
    }


    
public static void main(String[] args) throws IOException
    
{
        BufferedReader in 
= new BufferedReader(new InputStreamReader(System.in));
        
while (true)
        
{
            System.out.print(
"\n输入: ");
            String r
=in.readLine();
            
if (r.equals("end"))
            
{
                
break;
            }

            System.out.println(
"大写: "+display(r));
        }

    }

}
posted on 2007-05-06 23:33 W.Y.H 阅读(339) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: