金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出
package com.test; public class RMBConvert { public static void main(String[] args) { convertMoney(910000000); } private static final char[] units = { ‘元‘,‘拾‘,‘佰‘,‘仟‘,‘万‘,‘拾‘,‘佰‘,‘仟‘,‘亿‘ }; private static final char[] data = { ‘零‘,‘壹‘,‘贰‘,‘叁‘,‘肆‘,‘伍‘,‘陆‘,‘柒‘,‘捌‘,‘玖‘ }; /** * 金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出 * @param money * @return */ public static String convertMoney(int money) { int unitIndex = 0; int number = 0; StringBuffer sb = new StringBuffer(); while (money > 0) { sb.insert(0, units[unitIndex++]); System.out.println(sb.toString()); number = money % 10; money /= 10; sb.insert(0, data[number]); System.out.println(sb.toString()); } String res = sb.toString().replaceAll("零[仟佰拾]", "零").replaceAll("零+元", "").replaceAll("零+万", "万").replaceAll("零+", "零"); System.out.println(res); return res; } }