题解 | #人民币转换#

人民币转换

https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b

import java.math.BigDecimal;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {


    public static final String[] BIG = new String[] {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String[] split = input.split("\\.");

        long money = Long.parseLong(split[0]);
        int fen = Integer.parseInt(split[1]);

        StringBuilder sb = new StringBuilder();
        String result = "";

        if (money > 0) {
            convert(money, sb);
            result = sb.toString();
            while (result.contains("零零")) {
                result = result.replace("零零", "零");
            }

            if (result.startsWith("零")) {
                result = result.replaceFirst("零", "");
            }

            if (result.endsWith("零")) {
                result = result.substring(0, result.length() - 1);
            }

            result = result + "元";
        }

        sb = new StringBuilder();
        if (fen == 0) {
            sb.append("整");
        } else {
            if (fen >= 10) {
                sb.append(BIG[fen / 10]).append("角");
            } 
            if (fen % 10 != 0) {
                sb.append(BIG[fen % 10]).append("分");
            }
        }

        System.out.println("人民币" + result + sb.toString());

    }


    public static void convert(long money, StringBuilder sb) {

        long yi_num = money / 100000000;
        if (yi_num >= 1) {
            convert(yi_num, sb);
            sb.append("亿");
        }

        int qianwan = (int) (money / 10000000 % 10);
        int baiwan = (int) (money / 1000000 % 10);
        int shiwan = (int) (money / 100000 % 10);
        int wan = (int) (money / 10000 % 10);
        int qian = (int) (money / 1000 % 10);
        int bai = (int) (money / 100 % 10);
        int shi = (int) (money / 10 % 10);
        int ge = (int) (money % 10);

        if (qianwan == 0 && baiwan == 0 && shiwan == 0 && wan == 0) {
            sb.append(BIG[0]);
        } else {

            sb.append(BIG[qianwan]);
            if (qianwan != 0) {
                sb.append("仟");
            }

            sb.append(BIG[baiwan]);
            if (baiwan != 0) {
                sb.append("佰");
            }

            sb.append(BIG[shiwan]);
            if (shiwan != 0) {
                sb.append("拾");
            }

            if (wan != 0) {
                sb.append(BIG[wan]);
            }
            sb.append("万");

            if (shiwan == 0 && wan == 0) {
                sb.append(BIG[0]);
            }
        }

        sb.append(BIG[qian]);
        if (qian != 0) {
            sb.append("仟");
        }

        sb.append(BIG[bai]);
        if (bai != 0) {
            sb.append("佰");
        }


        if (shi > 0) {
            if(shi >1){
                sb.append(BIG[shi]);
            }
            sb.append("拾");
        }

        sb.append(BIG[ge]);
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务