java实现将阿拉伯数字转成中文大写工具类

package com.sqds.utils;

/**
 * <p>Title: money</p>
 * <p>Description: 
 *   *该类是把阿拉伯数字转换成中文大写的类。根据王大庆兄的C++程序稍做修改而成,后面附有王大庆兄的C++
 *   *程序,如果对所附的C++程序有什么问题请与王大庆兄联系email: wang_daqing@21cn.com
 *   *如果对java部分有什么看法和建议请与小弟联系,杨璇 email:netfalcon@263.net
 *   *类名:money
 *   *函数:String PositiveIntegerToHanStr(String NumStr)   负责把小数点前面的数转换为大写中文
 *    *函数:?String NumToRMBStr(double val)   负责把输入的double型的数转换为大写中文
 *   *注意java程序转换的范围是:小数点前面15位(已测试通过),C++程序是24位(我没有测试)
 * </p>
 * <p>Copyright: Copyright (c) 2003 你可以对本程序随意修改,复制,使用,但请保留这里注释声明!!!</p>
 * <p>Company: </p>
 * @author 王大庆、杨璇
 * @version 1.0
 */

import java.lang.Math;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MoneyUtils {	


	static String HanDigiStr[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

	static String HanDiviStr[] = new String[] { "", "拾", "佰", "仟", "万", "拾", "佰", "仟",
			"亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万",
			"拾", "佰", "仟" };

	public static String PositiveIntegerToHanStr(String NumStr) { // 输入字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零
		String RMBStr = "";
		boolean lastzero = false;
		boolean hasvalue = false; // 亿、万进位前有数值标记
		int len, n;
		len = NumStr.length();
		if (len > 15)
			return "数值过大!";
		for (int i = len - 1; i >= 0; i--) {
			if (NumStr.charAt(len - i - 1) == ' ')
				continue;
			n = NumStr.charAt(len - i - 1) - '0';
			if (n < 0 || n > 9)
				return "输入含非数字字符!";

			if (n != 0) {
				if (lastzero)
					RMBStr += HanDigiStr[0]; // 若干零后若跟非零值,只显示一个零
				// 除了亿万前的零不带到后面
				// if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) ) //
				// 如十进位前有零也不发壹音用此行
				//if (!(n == 1 && (i % 4) == 1 && i == len - 1)) // 十进位处于第一位不发壹音
					RMBStr += HanDigiStr[n];
				RMBStr += HanDiviStr[i]; // 非零值后加进位,个位为空
				hasvalue = true; // 置万进位前有值标记

			} else {
				if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) // 亿万之间必须有非零值方显示万
					RMBStr += HanDiviStr[i]; // “亿”或“万”
			}
			if (i % 8 == 0)
				hasvalue = false; // 万进位前有值标记逢亿复位
			lastzero = (n == 0) && (i % 4 != 0);
		}

		if (RMBStr.length() == 0)
			return HanDigiStr[0]; // 输入空字符或"0",返回"零"
		return RMBStr;
	}

	public static String NumToRMBStr(double val) {
		String SignStr = "";
		String TailStr = "";
		long fraction, integer;
		int jiao, fen;
		if (val < 0) {
			val = -val;
			SignStr = "负";
		}
		if (val > 99999999999999.999 || val < -99999999999999.999)
			return "数值位数过大!";
		// 四舍五入到分
		long temp = Math.round(val * 100);
		integer = temp / 100;
		fraction = temp % 100;
		jiao = (int) fraction / 10;
		fen = (int) fraction % 10;
		if (jiao == 0 && fen == 0) {
			TailStr = "整";
		} else {
			TailStr = HanDigiStr[jiao];
			if (jiao != 0)
				TailStr += "角";
			if (integer == 0 && jiao == 0) // 零元后不写零几分
				TailStr = "";
			if (fen != 0)
				TailStr += HanDigiStr[fen] + "分";
		}

		// 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分”
		// if( !integer ) return SignStr+TailStr;

		return SignStr + PositiveIntegerToHanStr(String.valueOf(integer))
				+ "元" + TailStr;
	}

	public static String NumToCHSStr(double val) {
		long integer;
		if (val > 99999999999999.999 || val < -99999999999999.999)
			return "数值位数过大!";
		// 四舍五入到分
		long temp = Math.round(val * 100);
		integer = temp / 100;
		return PositiveIntegerToHanStr(String.valueOf(integer));
	}

	public static BigDecimal sswr(BigDecimal ddd){
//		DecimalFormat df1 = new DecimalFormat("#");
//		String temp1 = df1.format(ddd);
//		ddd = new BigDecimal(temp1);
		if(ddd==null){
			return new BigDecimal(0);
		}
		DecimalFormat df2 = new DecimalFormat("#.####");
		String temp2 = df2.format(ddd);
		return new BigDecimal(temp2);
	}
	public static BigDecimal sswr1(BigDecimal ddd){
		if(ddd==null){
			return new BigDecimal(0);
		}
		DecimalFormat df2 = new DecimalFormat("#.00");
		String temp2 = df2.format(ddd);
		return new BigDecimal(temp2);
	}
	public static BigDecimal sswr2(BigDecimal ddd){
//		DecimalFormat df1 = new DecimalFormat("#");
//		String temp1 = df1.format(ddd);
//		ddd = new BigDecimal(temp1);
		if(ddd==null){
			return new BigDecimal(0);
		}
		DecimalFormat df2 = new DecimalFormat("#.####");
		String temp2 = df2.format(ddd);
		return new BigDecimal(temp2);
	}
	/**
	 * 显示结果为:千万元
	 * @param ddd
	 * @return
	 */
	public static String sswr3(BigDecimal ddd){
//		DecimalFormat df1 = new DecimalFormat("#");
//		String temp1 = df1.format(ddd);
//		ddd = new BigDecimal(temp1);
		ddd=ddd.divide(new BigDecimal(1000),7,BigDecimal.ROUND_HALF_DOWN);
		DecimalFormat df2 = new DecimalFormat("#.####");
		String temp2 = df2.format(ddd);
		return temp2;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值