题解 | #人民币转换#

人民币转换

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

import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner;

public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String line = scanner.nextLine(); double d = Double.parseDouble(line); long integerPart = (long) d; int decimalPart = (BigDecimal.valueOf(d).subtract(BigDecimal.valueOf(integerPart))).multiply(BigDecimal.valueOf(100)).intValue();

    int fen = decimalPart % 10;
    int jiao = (decimalPart -fen)/10;

    StringBuilder sb = new StringBuilder();
    sb.append("人民币");
    if (integerPart > 0) {
        sb.append(convert(getReadString(integerPart)));
    }

    if (decimalPart == 0) {
        sb.append("整");
    } else {
        if (jiao != 0) {
            sb.append(getRmbString(jiao)).append("角");
        }
        if (fen != 0) {
            sb.append(getRmbString(fen)).append("分");
        }
    }
    System.out.println(sb.toString());
}
}
public static String convert(List<String> data) {
    StringBuilder sb = new StringBuilder();
    for (int i = data.size() - 1; i >= 0; i--) {
        String num = data.get(i);
        StringBuilder per = new StringBuilder();
        for (int j = 0; j < num.length(); j++) {
            int no = Integer.parseInt(num.charAt(j) + ""); // 1 0 0 0
            if (j==num.length()-1 && no==0){
                continue;
            }else if(i!=0 && j==0 &&no==0){
                per.append("零");
            }else if (j!=0 && no==0 && per.charAt(per.length()-1)=='零'){
                continue;
            }
            if (num.length()==4 && j==2 && no==1){

            }else if (num.length()==2&&j==0&&no==1){

            }else{
                per.append(getRmbString(no));
            }

            if (no != 0) {
                per.append(getRmbSeat(num.length()- j - 2));
            }

        }
        if (per.charAt(per.length()-1) =='零'){
            per.deleteCharAt(per.length()-1);
        }
        sb.append(per);
        if (i==0){
            sb.append("元");
        }else if (i==1){
            sb.append("万");
        }else if (i==2){
            sb.append("亿");
        }
    }
    return sb.toString();
}

private static String rmbNumber = "零壹贰叁肆伍陆柒捌玖";
private static String rmbUnit = "元分";
private static String seat = "拾佰仟万亿";

public static String getRmbString(int i) {
    return rmbNumber.charAt(i) + "";
}

public static String getRmbSeat(int i) {
    if (i < 0) {
        return "";
    }
    return seat.charAt(i) + "";
}
    
//1250035004
public static List<String> getReadString(long money) {
    String moneyString = money + "";
    StringBuilder sb = new StringBuilder();
    List<String> data = new ArrayList<>();
    int size = moneyString.length();
    int k = 1;
    for (int i = size - 1; i >= 0; i--) {
        sb.append(moneyString.charAt(i));
        if (k != 1 && k % 4 == 0) {
            data.add(sb.reverse().toString());
            sb = new StringBuilder();
        }
        k++;
    }
    if (sb.length() > 0) {
        data.add(sb.reverse().toString());
    }
    return data;
}

}

全部评论

相关推荐

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