金额转换

    技术2022-05-20  60

    static String unit = "美元"; public static void main(String[] args) { // TODO Auto-generated method stub String[] item = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; String[] count = { unit, "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万", "十", "百", "千" }; long rmb = 100000000001l; String result = ""; int i = 0; String s = String.valueOf(rmb); int length = s.length(); if (length > count.length) { System.out.println("金额太大了 "); return; } // 获取最高位数值 int highest = (int) (rmb / Math.pow(10, length - 1)); while (rmb / 10 > 0) { int index = (int) (rmb % 10);// 得到各个位数上的数值 String tmp = ""; if (index == 0) { if ((!count[i].equals("万") && !count[i].equals("亿") && !count[i] .equals(unit))) { tmp = item[index]; } else { tmp = item[index] + count[i]; } } else { tmp = item[index] + count[i]; } rmb = rmb / 10; result = tmp + result; i++; } result = item[highest] + count[length - 1] + result; System.out.println(result); // 处理零 System.out.println(getString(result)); } public static String getString(String s) { s = s.replaceAll("(零{4})(亿)", "$2"); s = s.replaceAll("(零{4})(万|亿|" + unit + ")", ""); s = s.replaceAll("(零{3})(万|亿|" + unit + ")", "$2"); s = s.replaceAll("零{2}", "零"); s = s.replaceAll("(零)(万|亿|" + unit + ")", "$2"); s = s.replaceAll("零{2}", "零"); return s; }


    最新回复(0)