java代码如何把数字如1987.25元转化为壹仟玖佰捌拾柒元贰角伍分

解决方案 »

  1.   

    建立一个二维数组咯!然后讲大小写对应输入到二维数组当中,最后对这个float数字进行按位提取,分别提取万、千、百、十、元、角、分的各位,最后再组合成大写就可以了。
      

  2.   

    http://blog.csdn.net/davis129/archive/2006/08/27/1127374.aspx
      

  3.   

    have a try~public static String toChineseCurrency(Object o) {
    if(o instanceof Number) {
    String s = new DecimalFormat("#.00").format(o);
    System.out.println(s);
    s = s.replaceAll("\\.", "");
    char[] digit = {'零', '壹', '贰', '叁', '肆',
    '伍', '陆', '柒', '捌', '玖'};
    String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";
    int l = unit.length();
    StringBuilder sb = new StringBuilder(unit);
    for(int i=s.length()-1; i>=0; i--) 
    sb = sb.insert(l-s.length()+i, digit[(s.charAt(i) - 0x30)]);
    s = sb.substring(l-s.length(), l+s.length());
    s = s.replaceAll("零[拾佰仟]", "零").
    replaceAll("零{2,}", "零").
    replaceAll("零兆", "兆").
    replaceAll("零万", "万").
    replaceAll("零元", "元").
    replaceAll("零[角分]", "")
    ;
    return s;
    } else {
    throw new NumberFormatException();
    }
    }
      

  4.   

    这个正解.(zephyr_cc())我支持.
    ------------------------------------------------
    这个blog里的有bug(http://blog.csdn.net/davis129/archive/2006/08/27/1127374.aspx)
    我输入100035.53,输出(壹拾零叁拾伍圆伍角叁分)中间少了个万.应该是(壹拾万零叁拾伍圆伍角叁分).
      

  5.   

    package net.yilin.util;public class Transform{
    private double number;   //人民币金额
    private StringBuffer capitalize = new StringBuffer(); //人民币大写形式
    private int integer; //整数部分
    private int decimal = 0; //小数部分

    /*构造方法*/
    public Transform(double number){   
    this.number = number;
    init();
    }
    /*获取结果*/
    public String getString(){  
    return capitalize.toString();
    }
    /*打印接口*/
    public void display(){
    System.out.print("\n您输入数的人民币大写形式为:");
    System.out.println(getString()+"\n");
    }
    /*初始化*/
    private void init(){      
    String str1,str2;
    int index;

    integer = (int)number;

    str1 = Double.toString(number-integer)+"00";
    index = str1.indexOf('.');
    if(index != -1){
    str2 = str1.substring(index+1);
    decimal = (str2.charAt(0)-48)*10 + (str2.charAt(1)-48);
    }

    }

    /* 单位数字的转换 */
    private void digit(int i){
    switch(i){
    case 0 :
    capitalize.append("零");
    break;
    case 1 :
    capitalize.append("壹");
    break;
    case 2 :
    capitalize.append("贰");
    break;
    case 3 :
    capitalize.append("叁");
    break;
    case 4 :
    capitalize.append("肆");
    break;
    case 5 :
    capitalize.append("伍");
    break;
    case 6 :
    capitalize.append("陆");
    break;
    case 7 :
    capitalize.append("柒");
    break;
    case 8 :
    capitalize.append("捌");
    break;
    case 9 :
    capitalize.append("玖");
    break;
    default :
    throw new RuntimeException();    //测试所用
    }
    }
    /*十位数的转换*/
    private void ten(int x){
    if(x>=100){    //测试所用
    throw new RuntimeException();
    }
    int bit = x/10;
    digit(bit);
    capitalize.append("拾");
    x %= 10;
    if(x!=0){
    digit(x);
    }

    }
    /*百位数的转换*/
    private void hundred(int x){
    if(x>=1000){    //测试所用
    throw new RuntimeException();
    }
    int bit = x/100;
    digit(bit);
    capitalize.append("佰");
    x %= 100;
    if(x >= 10){
    ten(x);
    }else if(x!=0){
    digit(0);
    digit(x);
    }
    }
    /*千位数的转换*/
    private void kilo(int x){
    if(x>=10000){    //测试所用
    throw new RuntimeException();
    }
    int bit = x/1000;
    digit(bit);
    capitalize.append("仟");
    x %= 1000;
    if(x>=100){
    hundred(x);
    }else if(x>=10){
    digit(0);
    ten(x);
    }else if(x!=0){
    digit(0);
    digit(x);
    }
    }
    /*转换任何小于一万的数*/
    private void trans(int x){
    if(x>=10000){    //测试所用
    throw new RuntimeException();
    }
    if(x>=1000){
    kilo(x);
    }else if(x>=100){
    hundred(x);
    }else if(x>=10){
    ten(x);
    }else if(x!=0){
    digit(x);
    }
    }
    /*转换一亿以下、一万以上的数*/
    private void tenThousand(int x){
    if(x>=100000000){    //测试所用
    throw new RuntimeException();
    }
    int temp = x/10000;
    trans(temp);
    capitalize.append("万");
    x %= 10000;
    if(x>=1000){
    trans(x);
    }else if(x!=0){
    digit(0);
    trans(x);
    }
    }
    /*转换一亿以上的数*/
    private void hundredMillion(int x) throws NumberOutOfBoundsException{
    if(x>=2100000000){    //测试所用
    throw new NumberOutOfBoundsException();
    }
    int temp = x/100000000;
    trans(temp);
    capitalize.append("亿");
    x %= 100000000;
    if(x>=10000000){
    tenThousand(x);
    }else if(x>=10000){
    digit(0);
    tenThousand(x);
    }else if(x>=1000){
    digit(0);
    trans(x);
    }else if(x!=0){
    digit(0);
    trans(x);
    }
    }
    /*角与分的转换*/
    private void decimalDigits(){
    if(decimal>=10){
    int bit = decimal/10;
    digit(bit);
    capitalize.append("角");
    decimal %= 10;
    }
    if(decimal!=0){
    digit(decimal);
    capitalize.append("分");
    }
    }
    /*功能接口*/
    public void transform() throws NumberOutOfBoundsException{
    if(integer==0){
    decimalDigits();
    return;
    }
    if(integer>=100000000){
    hundredMillion(integer);
    }else if(integer>=10000){
    tenThousand(integer);
    }else{
    trans(integer);
    }
    capitalize.append("元");
    decimalDigits();
    }
    /*测试*/
    public static void main(String[] args) throws NumberOutOfBoundsException{
    double input = 102030720.65;
    Transform t = new Transform(input);
    t.transform();
    t.display();
    }
    }
      

  6.   

    package net.yilin.util;public class NumberOutOfBoundsException extends Exception{
            public NumberOutOfBoundsException (){
                super("所输入的数过大!");
            }
    }
      

  7.   

    import net.yilin.util.Transform;
    import net.yilin.util.NumberOutOfBoundsException;
    import java.io.*;public class Test {
        public static void main(String[] args){
            String select = "1" ;
            double input;
            
            while(select.equals("1")){
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader in = new BufferedReader(isr);
                
                System.out.print("\n请输入一个金额:");
                
                try{
                    input = Double.parseDouble(in.readLine());
                }catch(NumberFormatException e){
                    System.out.println("\n输入错误!,请重新输入!");
                    continue;
                }catch(IOException e){
                    System.out.println("输入错误!");
                    continue;
                }
                
                Transform t = new Transform(input);
                
    try{
                    t.transform();
                }catch(NumberOutOfBoundsException e){
                    System.out.println("\n您输入的数值过大,请重新输入!");
                    continue;
                }
                
                t.display();            System.out.println("==============================================");            do{
                 System.out.print("\n请选择功能:");
                 System.out.print("[  1、继续  0、退出  ]");
                try{
                    select = in.readLine();
                }catch(IOException e){
                    System.out.println("输入错误!");
                }
            }while(!select.equals("1")&&!select.equals("0"));
            
            try{
                    in.close();
                }catch(IOException e){
                    
                }
            
            }    }}
      

  8.   

    yilinhust:
    你的测试程序有问题,当输入1的时候程序会一直执行下去!因为在你第二个while后面你把输入流关闭了,所以读取金额抛异常!!
    把:try{
                    in.close();
                }catch(IOException e){
                    
                }
    去掉就行!!!
      

  9.   

    import net.yilin.util.Transform;
    import net.yilin.util.NumberOutOfBoundsException;
    import java.io.*;public class Test {
        public static void main(String[] args){
            String select = "1" ;
            double input;
            
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            
            while(select.equals("1")){
                
                System.out.print("\n请输入一个金额:");
                
                try{
                    input = Double.parseDouble(in.readLine());
                }catch(NumberFormatException e){
                    System.out.println("\n输入错误!,请重新输入!");
                    continue;
                }catch(IOException e){
                    System.out.println("输入错误!");
                    continue;
                }
                
                Transform t = new Transform(input);
                
    try{
                    t.transform();
                }catch(NumberOutOfBoundsException e){
                    System.out.println("\n您输入的数值过大,请重新输入!");
                    continue;
                }
                
                t.display();            System.out.println("==============================================");            do{
                 System.out.print("\n请选择功能:");
                 System.out.print("[  1、继续  0、退出  ]");
                try{
                    select = in.readLine();
                }catch(IOException e){
                    System.out.println("输入错误!");
                }
            }while(!select.equals("1")&&!select.equals("0"));
            }
            
            try{
                in.close();
            }catch(IOException e){
                
            }    }}