Java 数值转银行金额格式,并禁止四舍五入

这个博客主要介绍了如何使用Java实现金额格式化,包括将整数和小数转化为带有两位小数的字符串,并提供了向下取整的选项。此外,还详细展示了将银行金额转换为中文大写的方法,包括处理零元整、负数和四舍五入的逻辑,确保转换准确无误。
摘要由CSDN通过智能技术生成

金钱包含整数和小数,这里使用重载的方式

public class MoneyFmtUtils {

	/**
	 * 将整数转化为金额,返回两位小数
	 * 
	 * @param money
	 * @return String
	 */
	public static String moneyFormatToStr(Integer money) {
		if (money == null) {
			return null;
		}
		DecimalFormat mFmt = new DecimalFormat("###,##0.00");
		return mFmt.format(money);
	}

	/**
	 * 将小数转化为金额,返回两位小数
	 * 
	 * @param money
	 * @return String
	 */
	public static String moneyFormatToStr(Double money) {
		if (money == null) {
			return null;
		}
		DecimalFormat mFmt = new DecimalFormat("###,##0.00");
		mFmt.setRoundingMode(RoundingMode.DOWN); //向下取整,取消四舍五入
		return mFmt.format(money);
	}
	/**
	 * 将字符串类型的数值转化为银行金额格式
	 * @param money
	 * @return String
	 */
	public static String moneyFormatToStr(String money) {
		if (money == null || money == "") {
			return null;
		}
		return moneyFormatToStr(Double.parseDouble(money));	
	}

}

测试:

在这里插入图片描述


补充:如何将银行金额转化成中文大写格式
这里直接拿其他人的(给出链接,以示尊重)
https://blog.csdn.net/mexican_jacky/article/details/46653151

	// 汉语中数字大写
	private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	// 汉语中货币单位大写,这样的设计类似于占位符
	private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾",
			"佰", "仟", "兆", "拾", "佰", "仟" };
	// 特殊字符:整
	private static final String CN_FULL = "整";
	// 特殊字符:负
	private static final String CN_NEGATIVE = "负";
	// 金额的精度,默认值为2
	private static final int MONEY_PRECISION = 2;
	// 特殊字符:零元整
	private static final String CN_ZEOR_FULL = "零元" + CN_FULL;

	/**
	 * 将银行金额转化为大写
	 * @param numberOfMoney
	 * @return
	 */
	public static String numberFondsConvertToBigChars(String bankMoney) {
		BigDecimal numberOfMoney = new BigDecimal(bankMoney.replaceAll(",", ""));
		StringBuffer sb = new StringBuffer();
		int signum = numberOfMoney.signum();
		// 零元整的情况
		if (signum == 0) {
			return CN_ZEOR_FULL;
		}
		// 这里会进行金额的四舍五入
		long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();
		// 得到小数点后两位值
		long scale = number % 100;
		int numUnit = 0;
		int numIndex = 0;
		boolean getZero = false;
		// 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
		if (!(scale > 0)) {
			numIndex = 2;
			number = number / 100;
			getZero = true;
		}
		if ((scale > 0) && (!(scale % 10 > 0))) {
			numIndex = 1;
			number = number / 10;
			getZero = true;
		}
		int zeroSize = 0;
		while (true) {
			if (number <= 0) {
				break;
			}
			// 每次获取到最后一个数
			numUnit = (int) (number % 10);
			if (numUnit > 0) {
				if ((numIndex == 9) && (zeroSize >= 3)) {
					sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
				}
				if ((numIndex == 13) && (zeroSize >= 3)) {
					sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
				}
				sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
				sb.insert(0, CN_UPPER_NUMBER[numUnit]);
				getZero = false;
				zeroSize = 0;
			} else {
				++zeroSize;
				if (!(getZero)) {
					sb.insert(0, CN_UPPER_NUMBER[numUnit]);
				}
				if (numIndex == 2) {
					if (number > 0) {
						sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
					}
				} else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
					sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
				}
				getZero = true;
			}
			// 让number每次都去掉最后一个数
			number = number / 10;
			++numIndex;
		}
		// 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
		if (signum == -1) {
			sb.insert(0, CN_NEGATIVE);
		}
		// 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
		if (!(scale > 0)) {
			sb.append(CN_FULL);
		}
		return sb.toString();
	}

测试:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值