• 数字金额转换为汉字金额


    数字金额转换为汉字金额

    一、源代码:DoubleMoneyToChineseMoney.java

      1 package cn.com.zfc.example;
      2 
      3 import java.math.BigDecimal;
      4 import java.util.Scanner;
      5 
      6 /**
      7  * 数字转换为汉语中人民币的大写
      8  * 
      9  * @author zfc
     10  *
     11  */
     12 public class DoubleMoneyToChineseMoney {
     13     // 汉语中数字大写
     14     private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
     15     // 汉语中货币单位大写,这样的设计类似于占位符
     16     private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾",
     17             "佰", "仟", "兆", "拾", "佰", "仟" };
     18     // 特殊字符:整
     19     private static final String CN_FULL = "整";
     20     // 特殊字符:负
     21     private static final String CN_NEGATIVE = "负";
     22     // 金额的精度,默认值为2
     23     private static final int MONEY_PRECISION = 2;
     24     // 特殊字符:零元整
     25     private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
     26 
     27     /**
     28      * 把输入的金额转换为汉语中人民币的大写
     29      * 
     30      * @param numberOfMoney
     31      *            输入的金额
     32      * @return 对应的汉语大写
     33      */
     34     public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
     35         StringBuffer sb = new StringBuffer();
     36         // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
     37         // positive.
     38         int signum = numberOfMoney.signum();
     39         // 零元整的情况
     40         if (signum == 0) {
     41             return CN_ZEOR_FULL;
     42         }
     43         // 这里会进行金额的四舍五入
     44         long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();
     45         // 得到小数点后两位值
     46         long scale = number % 100;
     47         int numUnit = 0;
     48         int numIndex = 0;
     49         boolean getZero = false;
     50         // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
     51         if (!(scale > 0)) {
     52             numIndex = 2;
     53             number = number / 100;
     54             getZero = true;
     55         }
     56         if ((scale > 0) && (!(scale % 10 > 0))) {
     57             numIndex = 1;
     58             number = number / 10;
     59             getZero = true;
     60         }
     61         int zeroSize = 0;
     62         while (true) {
     63             if (number <= 0) {
     64                 break;
     65             }
     66             // 每次获取到最后一个数
     67             numUnit = (int) (number % 10);
     68             if (numUnit > 0) {
     69                 if ((numIndex == 9) && (zeroSize >= 3)) {
     70                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
     71                 }
     72                 if ((numIndex == 13) && (zeroSize >= 3)) {
     73                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
     74                 }
     75                 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
     76                 sb.insert(0, CN_UPPER_NUMBER[numUnit]);
     77                 getZero = false;
     78                 zeroSize = 0;
     79             } else {
     80                 ++zeroSize;
     81                 if (!(getZero)) {
     82                     sb.insert(0, CN_UPPER_NUMBER[numUnit]);
     83                 }
     84                 if (numIndex == 2) {
     85                     if (number > 0) {
     86                         sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
     87                     }
     88                 } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
     89                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
     90                 }
     91                 getZero = true;
     92             }
     93             // 让number每次都去掉最后一个数
     94             number = number / 10;
     95             ++numIndex;
     96         }
     97         // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
     98         if (signum == -1) {
     99             sb.insert(0, CN_NEGATIVE);
    100         }
    101         // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
    102         if (!(scale > 0)) {
    103             sb.append(CN_FULL);
    104         }
    105         return sb.toString();
    106     }
    107 
    108     public static void main(String[] args) {
    109         System.out.println("请输入一个数字金额:");
    110         Scanner scanner = new Scanner(System.in);
    111         double money = scanner.nextDouble();
    112         scanner.close();
    113         BigDecimal numberOfMoney = new BigDecimal(money);
    114         String string = DoubleMoneyToChineseMoney.number2CNMontrayUnit(numberOfMoney);
    115         System.out.println("你输入的数为:" + money + ",其对应汉字金额:" + string.toString());
    116     }
    117 }

    二、运行效果

  • 相关阅读:
    Markdown的简介(转)
    写在二月的尾巴上
    The Pragmatic Programmer 读书笔记
    C/C++语言的一些精简归纳
    一般常用设计模式及原则的思想小结
    常用UML模型简要小结
    LeetCode 101. Symmetric Tree
    LeetCode 100. Same Tree
    LeetCode 99. Recover Binary Search Tree
    线索二叉树的建立与遍历
  • 原文地址:https://www.cnblogs.com/zfc-java/p/7698340.html
Copyright © 2020-2023  润新知