金额大小写转换,金额转换为大写中文,大写金额转换为小写数字

金额大小写转换,金额转换为大写中文,大写金额转换为小写数字

经测试已经基本无BUG。

写之前对照别人代码进行了互相的金额转换测试。

并依此修正了自己的BUG。别人的代码还是有BUG的。

参考列表:

http://java.chinaitlab.com/net/744688.html

http://468030.blog.51cto.com/458030/94327

http://blog.163.com/zcy4123@126/blog/static/15525587720121325532523/

http://yoyo08.iteye.com/blog/572295

http://www.dewen.org/q/4673/%E9%87%91%E9%A2%9D%E5%A4%A7%E5%B0%8F%E5%86%99%E7%9A%84%E8%BD%AC%E6%8D%A2

其中很多都有BUG

 

import java.text.DecimalFormat;

/**
 * 
 * 金额大小写转换
 * 
 * @author mfanw
 * @time 2012-09-11
 */
public class Currency {

 public static String NUMB = "零壹贰叁肆伍陆柒捌玖";
 public static String UNIT = "元拾佰仟万拾佰仟亿拾佰仟";
 
 
 /**
  * 转换为大写(小数部分无效)
  * @param money 例如:1209900.00
  * @return
  */
 public static String toChinese(String money){
  /**
   * 1.确保是Double数值,并且含两位小数部分
   */
  DecimalFormat df = new DecimalFormat("########0.00");
  money = "" + df.format(Double.parseDouble(money));
  String ret = "";
  /**
   * 2.替换为中文数值(含单位)
   */
  String moneyI = money.substring(0, money.indexOf("."));//舍弃小数点及以后部分
  int i = 0;
  while (i < moneyI.length()) {
   int nowNumb = Integer.parseInt(""+moneyI.charAt(i));//获取数
   int nowUnit = moneyI.length()-i-1;//根据此值在整个数值中的位置获取单位
   if(nowNumb == 0){//零的情况:零后不用跟单位
    if(nowUnit%4==0){//万、亿为特殊单位,必加项
     ret += UNIT.charAt(nowUnit);
    } else if(ret.charAt(ret.length()-1) != '零'){//防止添加多个零
     ret += "零";
    }
   } else {//非零情况
    ret += NUMB.charAt(nowNumb);//添加数
    ret += UNIT.charAt(nowUnit);//添加位
   }
   i++;
  }
  /**
   * 3.标准化格式
   */
  ret = ret.replaceAll("零亿", "亿");
  ret = ret.replaceAll("零万", "万");
  ret = ret.replaceAll("零元", "元");
  if(ret.length() == 1){
   ret = "零元";
  }
  return ret;
 }
 
 /**
  * 转换为小写(小数部分无效)
  * @param money 例如:壹仟贰佰叁拾肆万伍仟陆佰柒拾捌元
  * @return
  */
 public static String toNumber(String money){
  double ret = 0, temp = 0;
  int nowNumb = 0,tInt = 0, i = 0, j = 0;
  while (i < money.length()) {
   tInt = NUMB.indexOf(money.charAt(i));
   if(tInt>-1){//数值情况
    nowNumb = tInt;
    if(tInt>0){//非零情况,零直接跳过,无须处理
     j = UNIT.indexOf(money.charAt(i+1));
     if(j>0 && j<4){//单位为 <拾佰仟> 的情况
      temp += nowNumb * Math.pow(10, j);
      i++;//下次循环跳过刚刚读过的 <拾佰仟> 单位
     }else{//单位为其他情况的,直接把结果暂存到 temp
      temp += nowNumb;
     }
    }
   }else{//非数值情况,将暂存temp根据单位存到ret中,并清空暂存temp
    if(money.charAt(i)=='亿'){
     ret += temp * 10000 * 10000;
     temp = 0;
    } else if(money.charAt(i)=='万'){
     ret += temp * 10000;
     temp = 0;
    } else if(money.charAt(i)=='元') {
     ret += temp;
     temp = 0;
    }
   }
   i++;
  }
  DecimalFormat df = new DecimalFormat("########0.00");
  return "" + df.format(ret);
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
//  System.out.println(NewChinese2.toChinese("10230398003"));
  System.out.println(Currency.toNumber(Currency.toChinese("21")));
  DecimalFormat df = new DecimalFormat("########0.00");
  String num1 = "";
  String str1 = "";
  String num2 = "";
  for(int i=Integer.MAX_VALUE/10000*9999;i<Integer.MAX_VALUE;i++){
   num1 = "" + df.format(i);
   str1 = Currency.toChinese(num1);
   num2 = Currency.toNumber(str1);
   if(num1.equals(num2)){
//    System.out.println(num1 + " ");
   }else{
    System.out.println(num1 + "\t" + num2);
   }
  }
  
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Java金额小写大写金额转换方法,我可以给你提供以下代码: ```java public class MoneyUtil { private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"}; private static final double MAX_VALUE = 9999999999999.99D; public static String digitUppercase(double money) { if (money < 0 || money > MAX_VALUE) { return ""; } long integral = (long) Math.floor(money); String integralStr = String.valueOf(integral); StringBuilder sb = new StringBuilder(); for (int i = 0; i < integralStr.length(); i++) { int number = Integer.parseInt(integralStr.substring(i, i + 1)); sb.append(CN_UPPER_NUMBER[number]).append(CN_UPPER_MONETRAY_UNIT[integralStr.length() - i - 1]); } if (sb.toString().endsWith("零元")) { sb.delete(sb.length() - 2, sb.length()); } sb.append("整"); int decimal = (int) Math.round((money - integral) * 100); if (decimal == 0) { sb.append("整"); } else { sb.append(CN_UPPER_NUMBER[decimal / 10]).append(CN_UPPER_MONETRAY_UNIT[1]) .append(CN_UPPER_NUMBER[decimal % 10]).append(CN_UPPER_MONETRAY_UNIT[0]); } return sb.toString(); } } ``` 这段代码可以将小写金额转换大写金额。你可以将要转换小写金额作为参数传递给该方法,然后该方法将返回转换后的大写金额。例如: ```java double money = 1234.56; String result = MoneyUtil.digitUppercase(money); System.out.println(result); ``` 输出结果为:壹仟贰佰叁拾肆元伍角陆分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值