import java.text.DecimalFormat;
public class MoneyUtil {
/*
* 将小写的人民币转化成大写
*/
public static String convertToChineseNumber(double number)
{
StringBuffer chineseNumber = new StringBuffer();
String [] num={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String [] unit = {"分","角","圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万"};
String tempNumber = String.valueOf(Math.round((number * 100)));
int tempNumberLength = tempNumber.length();
if ("0".equals(tempNumber))
{
return "零圆整";
}
if (tempNumberLength > 15)
{
try {
throw new Exception("超出转化范围.");
} catch (Exception e) {
e.printStackTrace();
}
}
boolean preReadZero = true; //前面的字符是否读零
for (int i=tempNumberLength; i>0; i--)
{
if ((tempNumberLength - i + 2) % 4 == 0)
{
//如果在(圆,万,亿,万)位上的四个数都为零,如果标志为未读零,则只读零,如果标志为已读零,则略过这四位
if (i - 4 >= 0 && "0000".equals(tempNumber.substring(i - 4, i)))
{
if (!preReadZero)
{
chineseNumber.insert(0, "零");
preReadZero = true;
}
i -= 3; //下面还有一个i--
continue;
}
//如果当前位在(圆,万,亿,万)位上,则设置标志为已读零(即重置读零标志)
preReadZero = true;
}
Integer digit = Integer.parseInt(tempNumber.substring(i - 1, i));
if (digit == 0)
{
//如果当前位是零并且标志为未读零,则读零,并设置标志为已读零
if (!preReadZero)
{
chineseNumber.insert(0, "零");
preReadZero = true;
}
//如果当前位是零并且在(圆,万,亿,万)位上,则读出(圆,万,亿,万)
if ((tempNumberLength - i + 2) % 4 == 0)
{
chineseNumber.insert(0, unit[tempNumberLength - i]);
}
}
//如果当前位不为零,则读出此位,并且设置标志为未读零
else
{
chineseNumber.insert(0, num[digit] + unit[tempNumberLength - i]);
preReadZero = false;
}
}
//如果分角两位上的值都为零,则添加一个“整”字
if (tempNumberLength - 2 >= 0 && "00".equals(tempNumber.substring(tempNumberLength - 2, tempNumberLength)))
{
chineseNumber.append("整");
}
return chineseNumber.toString();
}
public static String number2BigRMB(double sum) {
String[] numeral ={"十", "百", "千"};
String[] bigNumeral = {"亿", "万"};
String[] measure = {"元", "角", "分"};
String[] numbers ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
StringBuffer result = new StringBuffer();
int digit = 0;
for (double temp = sum; temp >= 1; temp /= 10) {
digit++;
}
int loop = 0;
for (int temp = digit - 4; temp > 0; temp -= 4) {
loop++;
}
int srcLoop = loop;
boolean acrossLoopZeroFlag = true;
while (loop >= 0) {
int doubleBigNumber1 = (int) (sum / Math.pow(10000, loop));
sum %= Math.pow(10000, loop);
int bigDigit = 0;
for (double temp = doubleBigNumber1; temp >= 1; temp /= 10) {
bigDigit++;
}
if (bigDigit < 4 && srcLoop != loop && acrossLoopZeroFlag) {
result.append(numbers[0]);
acrossLoopZeroFlag = false;
} else {
acrossLoopZeroFlag = true;
}
boolean flag = true;
if (loop > 0) {
flag = false;
}
boolean innerFlag = true;
if (doubleBigNumber1 == 0) {
innerFlag = false;
}
boolean addZeroFlag = false;
int i = (int) Math.pow(10, bigDigit - 1);
for (; i >= 1; i = i / 10, bigDigit--) {
int doubleNumber = doubleBigNumber1 / i;
if (doubleNumber > 0) {
if (addZeroFlag) {
result.append(numbers[0]);
addZeroFlag = false;
}
result.append(numbers[doubleNumber]);
if (bigDigit == 1) {
if (flag) {
result.append(measure[0]);
} else {
result.append(bigNumeral[loop % 2]);
}
} else {
result.append(numeral[bigDigit - 2]);
}
} else if (bigDigit == 1) {
if (flag) {
result.append(measure[0]);
} else {
if (innerFlag) {
result.append(bigNumeral[loop % 2]);
}
}
} else {
addZeroFlag = true;
}
doubleBigNumber1 %= i;
}
loop--;
}
DecimalFormat df = new DecimalFormat("#.##");
String floatStr = df.format(sum);
floatStr = floatStr.substring(floatStr.indexOf(".") + 1);
int floatPart = Integer.parseInt(floatStr);
int jiao = floatPart / 10;
int fen = floatPart % 10;
if (jiao > 0) {
result.append(numbers[jiao]);
result.append(measure[1]);
}
if (fen > 0) {
result.append(numbers[fen]);
result.append(measure[2]);
}
return result.toString();
}
}