Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164905
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-07-05 18:09:09




点击(此处)折叠或打开

  1. import java.math.BigDecimal;

  2. /**
  3.  * 金额工具类
  4.  * Created by beifang on 17/4/26.
  5.  */
  6. public class MoneyUtil {
  7.     /**
  8.      * 汉语中数字大写
  9.      */
  10.     private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆",
  11.             "伍", "陆", "柒", "捌", "玖"};
  12.     /**
  13.      * 汉语中货币单位大写,这样的设计类似于占位符
  14.      */
  15.     private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元",
  16.             "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
  17.             "佰", "仟"};
  18.     /**
  19.      * 特殊字符:整
  20.      */
  21.     private static final String CN_FULL = "整";
  22.     /**
  23.      * 特殊字符:负
  24.      */
  25.     private static final String CN_NEGATIVE = "负";
  26.     /**
  27.      * 金额的精度,默认值为2
  28.      */
  29.     private static final int MONEY_PRECISION = 2;
  30.     /**
  31.      * 特殊字符:零元整
  32.      */
  33.     private static final String CN_ZEOR_FULL = "零元" + CN_FULL;

  34.     /**
  35.      * 把输入的金额转换为汉语中人民币的大写
  36.      *
  37.      * @param numberOfMoney 输入的金额
  38.      * @return 对应的汉语大写
  39.      */
  40.     public static String number2Money(BigDecimal numberOfMoney) {
  41.         if(numberOfMoney == null) {
  42.             return null;
  43.         }
  44.         StringBuffer sb = new StringBuffer();
  45.         // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
  46.         // positive.
  47.         int signum = numberOfMoney.signum();
  48.         // 零元整的情况
  49.         if (signum == 0) {
  50.             return CN_ZEOR_FULL;
  51.         }
  52.         //这里会进行金额的四舍五入
  53.         long number = numberOfMoney.movePointRight(MONEY_PRECISION)
  54.                 .setScale(0, 4).abs().longValue();
  55.         // 得到小数点后两位值
  56.         long scale = number % 100;
  57.         int numUnit = 0;
  58.         int numIndex = 0;
  59.         boolean getZero = false;
  60.         // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
  61.         if (!(scale > 0)) {
  62.             numIndex = 2;
  63.             number = number / 100;
  64.             getZero = true;
  65.         }
  66.         if ((scale > 0) && (!(scale % 10 > 0))) {
  67.             numIndex = 1;
  68.             number = number / 10;
  69.             getZero = true;
  70.         }
  71.         int zeroSize = 0;
  72.         while (true) {
  73.             if (number <= 0) {
  74.                 break;
  75.             }
  76.             // 每次获取到最后一个数
  77.             numUnit = (int) (number % 10);
  78.             if (numUnit > 0) {
  79.                 if ((numIndex == 9) && (zeroSize >= 3)) {
  80.                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
  81.                 }
  82.                 if ((numIndex == 13) && (zeroSize >= 3)) {
  83.                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
  84.                 }
  85.                 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  86.                 sb.insert(0, CN_UPPER_NUMBER[numUnit]);
  87.                 getZero = false;
  88.                 zeroSize = 0;
  89.             } else {
  90.                 ++zeroSize;
  91.                 if (!(getZero)) {
  92.                     sb.insert(0, CN_UPPER_NUMBER[numUnit]);
  93.                 }
  94.                 if (numIndex == 2) {
  95.                     if (number > 0) {
  96.                         sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  97.                     }
  98.                 } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
  99.                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  100.                 }
  101.                 getZero = true;
  102.             }
  103.             // 让number每次都去掉最后一个数
  104.             number = number / 10;
  105.             ++numIndex;
  106.         }
  107.         // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
  108.         if (signum == -1) {
  109.             sb.insert(0, CN_NEGATIVE);
  110.         }
  111.         // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
  112.         if (!(scale > 0)) {
  113.             sb.append(CN_FULL);
  114.         }
  115.         return sb.toString();
  116.     }
  117. }

阅读(1040) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~