Java编程案例-将阿拉伯数字转换成中文数字

package org.lulu.learn.work;

import java.io.*;

/**
 * Project: Day07
 * Created: Lulu
 * Date: 2016/8/5
 */
public class Work02 {
    public static void main(String[] args) {
//        int num = 0;
//        System.out.println(tranWan(num));

        try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("res/data.txt")));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("res/result.txt")))
                ){

            String str = "";
            int percent = 0;

            while ((str = br.readLine()) != null) {
                bw.write(tranWan(Integer.parseInt(str)));
                bw.newLine();
                percent++;

                Thread.sleep(100);

                System.out.print("\r[");
                for (int i = 0; i < 20; i++) {
                    if(i < percent/5){
                        System.out.print("=");
                    }else if(i == percent/5){
                        System.out.print(">");
                    }else{
                        System.out.print(" ");
                    }

                }
                System.out.print("]");
                System.out.printf("\t%.2f%%", (float)percent);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    /**
     * 处理千万数字方法
     *
     * @param num
     * @return
     */
    private static String tranWan(int num) {
        StringBuilder builder = new StringBuilder();
        if (num / 10000 > 0) {//说明
            builder.append(trans(num / 10000)).append("万");
        }
        int temp = num % 10000;
        if (temp > 0) {
            String trans = trans(temp);
            //首先判断是否有万位,
            if (builder.length() > 0) {

                //如果千位为0, 则需要补零
                if (temp / 1000 == 0) {
                    builder.append("零");
                }
            }
            builder.append(trans);

        }
        if (builder.length() == 0) {
            builder.append("零");
        }
        return builder.toString();
    }

    /**
     * 完成4位数转换
     *
     * @param num
     * @return
     */
    private static String trans(int num) {
        String[] numeric = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        StringBuilder builder = new StringBuilder();
        builder.append(numeric[num / 1000] + "千").
                append(numeric[num / 100 % 10] + "百").
                append(numeric[num / 10 % 10] + "十").
                append(numeric[num % 10]);
        //去掉了零千....
        int index = -1;
        while ((index = builder.indexOf(numeric[0], index + 1)) != -1) {
            if (index < builder.length() - 1) {
                builder.deleteCharAt(index + 1);
            }
        }
        //去掉双零
        index = 0;
        while ((index = builder.indexOf("零零", index)) != -1) {
            builder.deleteCharAt(index);
        }

        if (builder.length() > 1) {
            //去掉开头的零
            if (builder.indexOf(numeric[0]) == 0) {
                builder.deleteCharAt(0);
            }
            //去掉末尾的零
            if (builder.indexOf(numeric[0]) == builder.length() - 1) {
                builder.deleteCharAt(builder.length() - 1);
            }

        }
        //把开头一十换成十
        if (builder.indexOf("一十") == 0) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值