闲来无事,突然想自己写个中文金额转换程序,想做到简洁高效优雅,看似简单的需求,做起来不一定就容易,特别是对于中间多个零的情况,还有单位换算。
package com.zsmud.ui;
import java.math.BigInteger;
import java.text.DecimalFormat;
/**
* 最大到九千兆之上
* @author Zhengsm
* @date 2012/12/10
*/
public class ToChineseAmount {
private static char[] chineseAmt = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
private static char[] units = {'拾','佰','仟'};//i=1,i=2,i=3,i=5,i=6,i=7,i=9,i=10,i=11
private static char[] chineseUnits = {'元','万','亿','兆'};//i=0,i=4,i=8,
private static char[] smallUnits = {'分','角'};
public static String formatInt(BigInteger num) {
if(num.equals(BigInteger.ZERO)) return "零元";
if(num.toString().length()>16) throw new RuntimeException("too max num");
StringBuffer result = new StringBuffer("");
BigInteger radix = new BigInteger("10");
int i=0;
char preChar = 0;
while(num.compareTo(BigInteger.ZERO)==1){
StringBuffer tmp = new StringBuffer();
BigInteger rest = num.mod(radix);
num = num.divide(radix);
if(rest.equals(BigInteger.ZERO) && preChar!='零' && inArray(chineseAmt,preChar)){
tmp.append('零');
}else if(!rest.equals(BigInteger.ZERO)){
tmp.append(chineseAmt[rest.intValue()]);
if(i%4!=0) tmp.append(units[i%4-1]);
}
if(i%4==0) {
if(tmp.length()>0 && tmp.charAt(0)=='零')
tmp.insert(0, chineseUnits[i/4]);
else
tmp.append(chineseUnits[i/4]);
}
preChar = tmp.length()>0? tmp.charAt(0):0;
i++;
result.insert(0, tmp);
}
return result.toString();
}
public static String formatSmall(int small){
if(small>100) throw new RuntimeException("too small");
StringBuffer result = new StringBuffer("");
char preChar = 0;
int i=0;
while(small>0){
StringBuffer tmp = new StringBuffer();
int rest = small%10;
small = small/10;
if(rest==0 && preChar!='零' && inArray(chineseAmt,preChar)){
tmp.append('零');
}else if(rest!=0){
tmp.append(chineseAmt[rest]);
tmp.append(smallUnits[i]);
}
preChar = tmp.length()>0? tmp.charAt(0):0;
i++;
result.insert(0, tmp);
}
return result.toString();
}
private static boolean inArray(char[] array,char c){
for(char ch : array){
if(ch == c) return true;
}
return false;
}
public static String format(double num){
DecimalFormat df = new DecimalFormat("#.00");
String input = df.format(num);
StringBuffer sb = new StringBuffer();
if(input.indexOf('.')!=-1) {
sb.append(formatInt(new BigInteger(input.substring(0, input.indexOf('.')))));
sb.append(formatSmall(Integer.parseInt(input.substring(input.indexOf('.')+1))));
}
return sb.toString();
}
public static void main(String[] args){
System.out.println(formatInt(new BigInteger("909")));
System.out.println(formatInt(new BigInteger("9009")));
System.out.println(formatInt(new BigInteger("9099")));
System.out.println(formatInt(new BigInteger("909099")));
System.out.println(formatInt(new BigInteger("909909")));
System.out.println(formatInt(new BigInteger("909999")));
System.out.println(formatInt(new BigInteger("90909999")));
System.out.println(formatInt(new BigInteger("900090019001921")));
System.out.println(formatInt(new BigInteger("900090009000")));
System.out.println(formatInt(new BigInteger("90909000")));
System.out.println(formatInt(new BigInteger("90999009")));
System.out.println(formatInt(new BigInteger("909009009")));
System.out.println(formatInt(new BigInteger("9009909009009")));
System.out.println(formatInt(new BigInteger("9009009909009009")));
System.out.println(formatSmall(78));
System.out.println(format(9999990999999.99));
System.out.println(format(9999990999999.00));
}
}