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

本文介绍了一个Java方法,用于将double类型的数值转换为符合中国规范的大写金额形式,并提供了详细的实现步骤及运行示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 --> 负壹仟贰佰零叁亿玖仟零贰拾贰万壹仟贰佰零伍元玖角玖分



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值