您的位置:首页 > 编程语言 > Java开发

Java实现金额大写,支持负数,不使用四舍五入的情况下保留两位小数

2017-10-20 00:44 676 查看
double类型的数值转金额大写的方法

public final static String toMoneyCHI(double num) {
String unitBitCHI[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}, {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}};
long intBit = (long)(Math.abs(num));
StringBuilder numStr = new StringBuilder(Math.abs(num) + "0");
int temp, tempMod, fourIndex = 0, floatBit, eIndex = numStr.indexOf("E");
if (eIndex > 0) {
String[] eArr = String.valueOf(num).split("E");
int eBit = Integer.parseInt(eArr[1]);
if (eBit < eArr[0].length() - 2) {
floatBit = ('E' != numStr.charAt(eBit + 2)) ? ((numStr.charAt(eBit + 2) - 48) * 10) : 0;
floatBit += ('E' != numStr.charAt(eBit + 3)) ? (numStr.charAt(eBit + 3) - 48) : 0;
} else {
floatBit = 0;
}
} else {
int pointIndex = numStr.indexOf(".");
floatBit = Integer.parseInt(numStr.substring(pointIndex + 1, pointIndex + 3));
}
StringBuilder sb = new StringBuilder();
do {
temp = (int)(intBit % 10000);
if (temp == 0) {
if (fourIndex == 0) {
sb.append(((num < 10000) ? unitBitCHI[2][0] : "") + unitBitCHI[0][fourIndex]);
}
} else {
int bitIndex = 0;
boolean preZero = true;
sb.insert(0, unitBitCHI[0][fourIndex]);
do {
tempMod = temp % 10;
if (tempMod == 0) {
if (!preZero) {
sb.insert(0, unitBitCHI[2][0]);
preZero = true;
}
} else {
sb.insert(0, unitBitCHI[1][bitIndex]);
sb.insert(0, unitBitCHI[2][tempMod]);
preZero = false;
}
bitIndex++;
temp /= 10;
} while (temp > 0);
if (bitIndex < 4 && intBit > 10000) {
sb.insert(0, unitBitCHI[2][0]);
}
}
fourIndex++;
intBit /= 10000;
} while (intBit > 0);
if (floatBit == 0) {
sb.append("整");
} else {
sb.append(floatBit / 10 == 0 ? "" : unitBitCHI[2][floatBit / 10] + "角");
sb.append(floatBit % 10 == 0 ? "" : unitBitCHI[2][floatBit % 10] + "分");
}
return sb.insert(0, (num < 0) ? "负" : "").toString();
}

运行
public static void main(String args[]) {
System.out.println("0.00 --> " + toMoneyCHI(0.00));
System.out.println("1.10 --> " + toMoneyCHI(1.10));
System.out.println("10.01 --> " + toMoneyCHI(10.01));
System.out.println("100.11 --> " + toMoneyCHI(100.11));
System.out.println("1000.05 --> " + toMoneyCHI(1000.05));
System.out.println("10000.01 --> " + toMoneyCHI(10000.01));
System.out.println("10000000.015 --> " + toMoneyCHI(10000000.015));
System.out.println("100000000.025 --> " + toMoneyCHI(100000000.025));
System.out.println("10002000. --> " + toMoneyCHI(10002000.));
System.out.println("100100100.1 --> " + toMoneyCHI(100100100.1));
System.out.println("110110110.21 --> " + toMoneyCHI(110110110.21));
System.out.println("110111032.989 --> " + toMoneyCHI(110111032.989));
System.out.println("120390221205.096 --> " + toMoneyCHI(120390221205.096));
System.out.println("-100000000005.904 --> " + toMoneyCHI(-100000000005.904));
System.out.println("-120390221205.994 --> " + toMoneyCHI(-120390221205.994));
}


运行结果
0.00 --> 零元整
1.10 --> 壹元壹角
10.01 --> 壹拾元壹分
100.11 --> 壹佰元壹角壹分
1000.05 --> 壹仟元伍分
10000.01 --> 壹万元壹分
10000000.015 --> 壹仟万元壹分
100000000.025 --> 壹亿元贰分
10002000. --> 壹仟万贰仟元整
100100100.1 --> 壹亿零壹拾万零壹佰元壹角
110110110.21 --> 壹亿壹仟零壹拾壹万零壹佰壹拾元贰角壹分
110111032.989 --> 壹亿壹仟零壹拾壹万壹仟零叁拾贰元玖角捌分
120390221205.096 --> 壹仟贰佰零叁亿玖仟零贰拾贰万壹仟贰佰零伍元玖分
-100000000005.904 --> 负壹仟亿零伍元玖角
-120390221205.994 --> 负壹仟贰佰零叁亿玖仟零贰拾贰万壹仟贰佰零伍元玖角玖分
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息