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 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 --> 负壹仟贰佰零叁亿玖仟零贰拾贰万壹仟贰佰零伍元玖角玖分
相关文章推荐
- java实现四舍五入并且保留两位小数
- 解决Double类型的金额字段中使用保留两位小数并四舍五入的问题
- java中保留两位小数(四舍五入后)
- C#中小数点后保留两位小数,四舍五入的函数及使用方法
- Java使用千分位并保留两位小数
- JAVA保留两位小数(四舍五入)
- JAVA笔记:double四舍五入并保留两位小数的方法
- java计算小数时保留两位小数,不采用四舍五入
- java数字保留两位小数四舍五入
- 怎样实现四舍五入,保留小数点后两位小数?
- java 小数四舍五入 保留两位小数
- Java 之四舍五入保留指定位小数实现
- java 金额计算,商业计算 double不精确问题 BigDecimal,Double保留两位小数方法
- java四舍五入保留两位小数方法整理
- C#中小数点后保留两位小数,四舍五入的函数及使用方法(转)
- C#中小数点后保留两位小数,四舍五入的函数及使用方法
- Java四舍五入保留两位小数
- java四舍五入保留两位小数方法整理
- 保留一个实数的两位小数,从第三位实现四舍五入
- js保留两位小数使用toFixed实现