package com.baiwang.zbxjf.utils;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class ChineseCapitalNumber {
public static String[] chineseDigits = new String[]{
"\u96f6", "\u58f9", "\u8d30", "\u53c1", "\u8086", "\u4f0d", "\u9646", "\u67d2",
"\u634c", "\u7396"};
private static final BigDecimal MAX = new BigDecimal(99999999999999.99d);
private static final BigDecimal MIN = new BigDecimal(-99999999999999.99d);
private static final BigDecimal big100 = new BigDecimal(100);
private static final BigDecimal big10000 = new BigDecimal(10000);
/**
* Convers amount number into Chinese capital number, keeps 2 digits of decimal part by round
*
* @param sAmount
* @return Capital Number in Chinese
*/
public static String amount2Chinese(String sAmount) {
BigDecimal amount = new BigDecimal(sAmount);
if (amount.compareTo(MAX) > 0 || amount.compareTo(MIN) < 0)
throw new IllegalArgumentException(
"parameter overflows (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;
if (amount.compareTo(BigDecimal.ZERO) < 0) {
negative = true;
amount = amount.negate();
}
BigDecimal temp = amount.multiply(big100).setScale(0, RoundingMode.HALF_UP);
int numFen = temp.remainder(BigDecimal.TEN).intValue(); // cent
temp = temp.divideToIntegralValue(BigDecimal.TEN);
int numJiao = temp.remainder(BigDecimal.TEN).intValue(); // dime
temp = temp.divideToIntegralValue(BigDecimal.TEN);
//temp is the integer part
int[] parts = new int[20];
int numParts = 0;
for (int i = 0; ; i++) {
if (temp.compareTo(BigDecimal.ZERO) == 0)
break;
int part = temp.remainder(big10000).intValue();
parts[i] = part;
numParts++;
temp = temp.divideToIntegralValue(big10000);
}
boolean beforeWanIsZero = true; // 10 thousand
String chineseStr = "";
for (int i = 0; i < numParts; i++) {
String partChinese = partTranslate(parts[i]);
if (i % 2 == 0) {
if ("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if (i != 0) {
if (i % 2 == 0)
chineseStr = "\u4ebf" + chineseStr;
else {
if ("".equals(partChinese) && !beforeWanIsZero)
chineseStr = "\u96f6" + chineseStr;
else {
if (parts[i - 1] < 1000 && parts[i - 1] > 0)
chineseStr = "\u96f6" + chineseStr;
chineseStr = "\u4e07" + chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if ("".equals(chineseStr))
chineseStr = chineseDigits[0];
else if (negative)
chineseStr = "\u8d1f" + chineseStr;
chineseStr = chineseStr + "\u5143";
if (numFen == 0 && numJiao == 0) {
chineseStr = chineseStr + "\u6574";
} else if (numFen == 0) {
chineseStr = chineseStr + chineseDigits[numJiao] + "\u89d2";
} else {
if (numJiao == 0)
chineseStr = chineseStr + "\u96f6" + chineseDigits[numFen] + "\u5206";
else
chineseStr = chineseStr + chineseDigits[numJiao] + "\u89d2"
+ chineseDigits[numFen] + "\u5206";
}
return chineseStr;
}
/**
* Turns a integer which is within range 0~9999 into Chinese capital number,return "" when it is 0
*
* @param amountPart
* @return
*/
private static String partTranslate(int amountPart) {
if (amountPart < 0 || amountPart > 10000) {
throw new IllegalArgumentException("parameter has to be an integer between 0 and 10000");
}
String[] units = new String[]{"", "\u62fe", "\u4f70", "\u4edf"};
int temp = amountPart;
String amountStr = new Integer(amountPart).toString();
int amountStrLength = amountStr.length();
boolean lastIsZero = true;
String chineseStr = "";
for (int i = 0; i < amountStrLength; i++) {
if (temp == 0)
break;
int digit = temp % 10;
if (digit == 0) {
if (!lastIsZero)
chineseStr = "\u96f6" + chineseStr;
lastIsZero = true;
} else {
chineseStr = chineseDigits[digit] + units[i] + chineseStr;
lastIsZero = false;
}
temp = temp / 10;
}
return chineseStr;
}
public static void main(String args[]) throws UnsupportedEncodingException{
System.out.println(amount2Chinese("123134"));
}
}