金额数字转换成中文写法

17 篇文章 0 订阅
13 篇文章 0 订阅
import java.math.BigDecimal;

/**
 * <p>
 * Title: 金额数字转换成中文写法
 * </p>
 * <p>
 * Description: 金额数字转换成中文写法
 * </p>
 */
public class ChineseCapital {

 static String[] hzUnit = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰",
   "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };

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

 /***************************************************************************
  * 金额转大写(后面加整)
  * 
  * @param Num
  * @return
  */
 public static String toChineseCapitalMoney(BigDecimal Num) {

  Num = Num.multiply(new BigDecimal("100"));
  Num=Num.setScale(2);
  // Num.movePointRight(2);
  String lwCase = Num.toString();

  int nPointPos = lwCase.indexOf('.');

  if (nPointPos == -1)
   nPointPos = lwCase.length();

  lwCase = lwCase.substring(0, nPointPos);

  lwCase = lwCase.trim();

  int nLen = lwCase.length();
  int iAddZero = 0;
  String szChMoney = "";

  String strDigital = "";
  int nDigital = 0;

  if (nLen > 15 || nLen == 0 || Num.doubleValue() < 0)
   return ""; // 数据错误返回

  for (int i = 0; i < nLen; i++) {
   strDigital = lwCase.substring(i, i + 1);
   nDigital = new Integer(strDigital).intValue();

   if (nDigital == 0) {
    iAddZero++;
   } else {
    if (iAddZero > 0) {
     szChMoney += "零";
    }

    szChMoney += hzNum[nDigital];

    iAddZero = 0;
   }

   if (nDigital != 0 || nLen - i == 3 || nLen - i == 11
     || ((nLen - i + 1) % 8 == 0 && iAddZero < 4)) // 该位不为0||元位||亿位||万位
    szChMoney += hzUnit[nLen - i - 1];
  }

  if (lwCase.endsWith("0"))
   szChMoney += "整";

  // if(szNum.Right(2)==_T("00")) szChMoney+=_T("整");

  return szChMoney;

 }

 /***************************************************************************
  * 金额转大写(后面不加整)
  * 
  * @param Num
  * @return
  */
 public static String toChineseCapitalMoney1(BigDecimal Num) {

  Num = Num.multiply(new BigDecimal("100"));
  Num=Num.setScale(2);
  // Num.movePointRight(2);
  String lwCase = Num.toString();

  int nPointPos = lwCase.indexOf('.');

  if (nPointPos == -1)
   nPointPos = lwCase.length();

  lwCase = lwCase.substring(0, nPointPos);

  lwCase = lwCase.trim();

  int nLen = lwCase.length();
  int iAddZero = 0;
  String szChMoney = "";

  String strDigital = "";
  int nDigital = 0;

  if (nLen > 15 || nLen == 0 || Num.doubleValue() < 0)
   return ""; // 数据错误返回

  for (int i = 0; i < nLen; i++) {
   strDigital = lwCase.substring(i, i + 1);
   nDigital = new Integer(strDigital).intValue();

   if (nDigital == 0) {
    iAddZero++;
   } else {
    if (iAddZero > 0) {
     szChMoney += "零";
    }

    szChMoney += hzNum[nDigital];

    iAddZero = 0;
   }

   if (nDigital != 0 || nLen - i == 3 || nLen - i == 11
     || ((nLen - i + 1) % 8 == 0 && iAddZero < 4)) // 该位不为0||元位||亿位||万位
    szChMoney += hzUnit[nLen - i - 1];
  }
  return szChMoney;

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println(ChineseCapital
    .toChineseCapitalMoney1(new BigDecimal("2")));//贰元
  System.out.println(ChineseCapital.toChineseCapitalMoney(new BigDecimal(
    "324129.21")));//叁拾贰万肆仟壹佰贰拾玖元贰角壹分
 }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值