分为整数部分和小数部分,整数部分利用replaceAll()和正则,去除多余的“零”。

import java.util.*;
public class Main {
    static Map<Character, String> numMap = new HashMap<>();
    static Map<Integer, String> lMap = new HashMap<>();
    static {
        numMap.put('1', "壹");
        numMap.put('2', "贰");
        numMap.put('3', "叁");
        numMap.put('4', "肆");
        numMap.put('5', "伍");
        numMap.put('6', "陆");
        numMap.put('7', "柒");
        numMap.put('8', "捌");
        numMap.put('9', "玖");
        numMap.put('0', "零");
        lMap.put(1, "元");
        lMap.put(2, "拾");
        lMap.put(3, "佰");
        lMap.put(4, "仟");
        lMap.put(5, "万");
        lMap.put(9, "亿");
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String[] ss = sc.nextLine().split("\\.");
            
            System.out.println("人民币" + zheng(ss[0].toCharArray()) + ling(ss[1]));

        }
    }

    public static String zheng(char[] chs) {
        StringBuilder sb = new StringBuilder();
        int n = chs.length;
        if (n == 1 && chs[0] == '0') return "";
        for (int i = 0; i < n; i++) {
            if (chs[i] == '0') {
                if (n > 5 && n - i == 5 || n - i == 1 || n - i == 9) {
                    sb.append(lMap.get(n - i));
                } else {
                    sb.append(numMap.get(chs[i]));
                }
            } else {
                if (n - i > 5 && n - i < 9) {
                    sb.append(numMap.get(chs[i]) + lMap.get((n - i) % 4));
                } else if (n - i > 9) {
                    sb.append(numMap.get(chs[i]) + lMap.get((n - i) % 8));
                } else {
                    sb.append(numMap.get(chs[i]) + lMap.get(n - i));
                }
            }
        }
        return sb.toString().replaceAll("[零]+", "零").replace("零万", "万").replace("壹拾", "拾");
    }

    public static String ling(String s) {
        StringBuilder res2 = new StringBuilder();
        if (s.equals("00")) {
            res2.append("整");
        } else {
            if (s.charAt(0) == '0' || s.charAt(1) == '0') {
                if (s.charAt(0) == '0') {
                    res2.append(numMap.get(s.charAt(1)) + "分");
                } else {
                    res2.append(numMap.get(s.charAt(0)) + "角");
                }
            } else {
                res2.append(numMap.get(s.charAt(0)) + "角" + numMap.get(s.charAt(
                                1)) + "分");
            }
        }
        return res2.toString();
    }
}