当前位置: 移动技术网 > IT编程>开发语言>Java > java数字转汉字工具类详解

java数字转汉字工具类详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java数字转汉字工具类的具体代码,供大家参考,具体内容如下

/**
 * created by 33303 on 2017/7/28.
 */
import java.math.bigdecimal;

/**
 * 数字转换为汉语中人民币的大写<br>
 *
 */
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 = "负";
 /**
 * 金额的精度,默认值为2
 */
 private static final int money_precision = 2;
 /**
 * 特殊字符:零元整
 */
 private static final string cn_zeor_full = "零元" + cn_full;

 /**
 * 把输入的金额转换为汉语中人民币的大写
 *
 * @param numberofmoney
 *  输入的金额
 * @return 对应的汉语大写
 */
 public static string number2cnmontrayunit(bigdecimal numberofmoney) {
 stringbuffer sb = new stringbuffer();
 // -1, 0, or 1 as the value of this bigdecimal is negative, zero, or
 // positive.
 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;
 // 判断最后两位数,一共有四中情况: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 (!(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);
 }
 // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
 if (!(scale > 0)) {
  sb.append(cn_full);
 }
 return sb.tostring();
 }

 public static void main(string[] args) {
 double money = 2020004.01;
 bigdecimal numberofmoney = new bigdecimal(money);
 string s = numbertocn.number2cnmontrayunit(numberofmoney);
 system.out.println("你输入的金额为:【"+ money +"】 #--# [" +s.tostring()+"]");
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网