将数字金额转换成大写金额

package com.wondersgroup.procomponent;

import java.math.BigDecimal;

public class Transformation {
	/**
	 * 汉语中数字大写
	 */
	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 = "零元";
	
	
	public static String number2CNMontrayUnit(BigDecimal numerOfMoney){
		StringBuffer sb = new StringBuffer();
		//返回-1,表示该数小于0,  0:表示该数等于0, 1:表示该数大于0
		int signum = numerOfMoney.signum();
		//零元的情况
		if(signum == 0){
			return CN_ZEOR_FULL;
		}
		/**
		 * 它等效于将该值的小数点向右移动n位。如果n为非负,则呼叫仅仅减去n。如果n为负,则调用等效于movePointLeft(-N)
		 * 金额一会进行四舍五入
		 */
		long number = numerOfMoney.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(numIndex != 0 && numIndex != 1 && numIndex != 2 && numIndex != 6 &&numIndex != 10 && numIndex != 14){
					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);
		}
		//除了0.00其他数据都带特殊字符:整
		sb.append(CN_FULL);
		//返回
		return sb.toString();
		
	}
	
	public static void main(String[] arge){
		double money = -12.34;
		BigDecimal bigDecimal = new BigDecimal(money);
		System.out.println("你输入的金额为" +money+"输出大写金额    
        为"+Transformation.number2CNMontrayUnit(bigDecimal).toString());
	}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALucianX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值