题解 | #人民币转换#

人民币转换

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] split = input.split("\\.");
        String integer = split[0];
        String decimal = split[1];
        String result = getInteger(integer) + getDecimal(decimal);
        System.out.println("人民币" + result);
    }

    private static String getInteger(String integer) {
        if("0".equals(integer)){
            return "";
        }
        long l = Long.parseLong(integer);
        String[] power = {"", "万", "亿"};
        String[] numOfChinese = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        // 当前的数量级
        int pow = 0;
        // 结果集,此时为倒序,从低位到高位存
        StringBuffer result = new StringBuffer();
        while (l != 0){
            // 当l不为0时取后四位
            int temp = (int)(l % 10000);
            if(temp != 0){
                // 后四位不为0时,存入当前数量级(""/"万"/"亿")
                result.append(power[pow]);
            }
            // 取个、十、百、千位
            int gewei = temp % 10;
            int shiwei = (temp / 10) % 10;
            int baiwei = (temp / 100) % 10;
            int qianwei = temp / 1000;
            // 个位不为0,直接存入
            if(gewei != 0){
                result.append(numOfChinese[gewei]);
            }
            // 处理十位
            if(shiwei != 0){
                result.append("拾");
                if(shiwei != 1){
                    result.append(numOfChinese[shiwei]);
                }
            } else {
                if(gewei != 0 && (temp > 99 || l > 10000)){
                    result.append(numOfChinese[0]);
                }
            }
            // 处理百位
            if(baiwei != 0){
                result.append("佰");
                result.append(numOfChinese[baiwei]);
            } else {
                if(shiwei != 0 && (temp > 999 || l > 10000)){
                    result.append(numOfChinese[0]);
                }
            }
            // 处理千位
            if(qianwei != 0){
                result.append("仟");
                result.append(numOfChinese[qianwei]);
            } else {
                if(baiwei != 0 && l > 10000){
                    result.append(numOfChinese[0]);
                }
            }
            // 处理下一个四位
            l = l / 10000;
            // 数量级+1
            pow++;
            if(pow > 2){
                // "亿"后面的数量级是"万",而不是""。之后都是"亿"和"万"交替
                pow = 1;
            }
        }
        return result.reverse().append("元").toString();
    }

    private static String getDecimal(String decimal) {
        if("00".equals(decimal)){
            return "整";
        }
        String[] numOfChinese = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        StringBuffer result = new StringBuffer();
        char c1 = decimal.charAt(0);
        char c2 = decimal.charAt(1);
        if(c1 != '0'){
            result.append(numOfChinese[c1 - '0']);
            result.append("角");
        }
        if (c2 != '0'){
            result.append(numOfChinese[c2 - '0']);
            result.append("分");
        }
        return result.toString();
    }
}

全部评论

相关推荐

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