收集这道题目原因是以前做过,但是实现的很麻烦,这次看到别人写的感觉简单易懂。
从一个pdf看到,出处就不贴了 = 。=
public class RenMingBi { private static final char[] data = new char[] { ‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘ }; private static final char[] units = new char[] { ‘元‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘万‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘亿‘,‘拾‘, ‘佰‘, ‘仟‘}; // 简单那整数 int:-2147483648~2147483647 public static String convert(long money) { StringBuffer sbf = new StringBuffer(40); int unit = 0; while (money != 0) { sbf.insert(0,units[unit++]); int number = (int) money % 10; sbf.insert(0, data[number]); money /= 10; } // System.out.println(sbf.toString()); return sbf.toString().replaceAll("零[拾佰仟]", "零").replaceAll("零+亿","亿").replaceAll("零+万", "万") .replaceAll("零+元", "元").replaceAll("零+", "零").replaceAll("亿万", "亿"); } //复杂带小数 long :-2^63 +2^63-1 public static String convert(double value) { long midVal = (long) (value * 100); String valStr = String.valueOf(midVal); // 转化成字符串 String rail = valStr.substring(valStr.length() - 2); // 取小数部分 long preint = (midVal / 100); String prefix = convert(preint); String suffix = ""; // 处理小数点后面的数 if (rail.equals("00")) { suffix = "整"; } else { suffix = data[rail.charAt(0) - ‘0‘] + "角" + data[rail.charAt(1) - ‘0‘] + "分"; } return prefix + suffix; // 返回正确表示 } public static void main(String[] args) { System.out.println(convert(2000000007)); System.out.println(convert(2000000007.211)); } }
时间: 2024-12-19 17:34:05