zoukankan      html  css  js  c++  java
  • 阿拉伯数字金额转换为大写

    import java.math.BigDecimal;
    
    public class NumberToCN {
    	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 = "负";
    	private static final int MONEY_PRECISION = 2;
    	private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
    	
    	public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
    		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;
    		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 / 10;
    		++numIndex;
    		}
    		if (signum == -1) {
    			sb.insert(0, CN_NEGATIVE);
    		}
    		if (!(scale > 0)) {
    			sb.append(CN_FULL);
    		}
    		return sb.toString();
    	}
    }
    

      

  • 相关阅读:
    java.util.date java.sql.date java.sql.timestamp
    javadoc生成文档时,编码 GBK 的不可映射字符
    java关于ServletConfig FilterConfig什么用
    在项目中用run as java aplication调试类
    replace和replaceAll
    在easyui中的datagrid中使用行内编辑时textarea的换行保存到mysql数据库为\n
    [转] 设计模式另类版
    [转] 不错的俄罗斯方块程序代码(VC++版)
    [转] 30道模拟经典题(JDK1.4)(附解答)
    [转] C#排序算法
  • 原文地址:https://www.cnblogs.com/fg-fd/p/7308361.html
Copyright © 2011-2022 走看看