java人民币大小写转换函数_java开发_数字转换汉语中人民币的大写_完整版
1 package com.b510.number2char;2
3 import java.math.BigDecimal;4
5 /**6 * 數(shù)字轉(zhuǎn)換為漢語(yǔ)中人民幣的大寫(xiě)
7 *8 * @author hongten9 * @contact hongtenzone@foxmail.com10 * @create 2013-08-1311 */12 public class NumberToCN {13 /**14 * 漢語(yǔ)中數(shù)字大寫(xiě)15 */16 private static final String[] CN_UPPER_NUMBER = { "零", "壹", "貳", "叁", "肆",17 "伍", "陸", "柒", "捌", "玖" };18 /**19 * 漢語(yǔ)中貨幣單位大寫(xiě),這樣的設(shè)計(jì)類似于占位符20 */21 private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",22 "拾", "佰", "仟", "萬(wàn)", "拾", "佰", "仟", "億", "拾", "佰", "仟", "兆", "拾",23 "佰", "仟" };24 /**25 * 特殊字符:整26 */27 private static final String CN_FULL = "整";28 /**29 * 特殊字符:負(fù)30 */31 private static final String CN_NEGATIVE = "負(fù)";32 /**33 * 金額的精度,默認(rèn)值為234 */35 private static final int MONEY_PRECISION = 2;36 /**37 * 特殊字符:零元整38 */39 private static final String CN_ZEOR_FULL = "零元" + CN_FULL;40
41 /**42 * 把輸入的金額轉(zhuǎn)換為漢語(yǔ)中人民幣的大寫(xiě)43 *44 * @param numberOfMoney45 * 輸入的金額46 * @return 對(duì)應(yīng)的漢語(yǔ)大寫(xiě)47 */48 public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {49 StringBuffer sb = new StringBuffer();50 // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or51 // positive.52 int signum = numberOfMoney.signum();53 // 零元整的情況54 if (signum == 0) {55 return CN_ZEOR_FULL;56 }57 //這里會(huì)進(jìn)行金額的四舍五入58 long number = numberOfMoney.movePointRight(MONEY_PRECISION)59 .setScale(0, 4).abs().longValue();60 // 得到小數(shù)點(diǎn)后兩位值61 long scale = number % 100;62 int numUnit = 0;63 int numIndex = 0;64 boolean getZero = false;65 // 判斷最后兩位數(shù),一共有四中情況:00 = 0, 01 = 1, 10, 1166 if (!(scale > 0)) {67 numIndex = 2;68 number = number / 100;69 getZero = true;70 }71 if ((scale > 0) && (!(scale % 10 > 0))) {72 numIndex = 1;73 number = number / 10;74 getZero = true;75 }76 int zeroSize = 0;77 while (true) {78 if (number <= 0) {79 break;80 }81 // 每次獲取到最后一個(gè)數(shù)82 numUnit= (int)(number % 10);83 if (numUnit>0) {84 if ((numIndex == 9) && (zeroSize >= 3)) {85 sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);86 }87 if ((numIndex == 13) && (zeroSize >= 3)) {88 sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);89 }90 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);91 sb.insert(0, CN_UPPER_NUMBER[numUnit]);92 getZero = false;93 zeroSize = 0;94 } else {95 ++zeroSize;96 if (!(getZero)) {97 sb.insert(0, CN_UPPER_NUMBER[numUnit]);98 }99 if (numIndex == 2) {100 if (number > 0) {101 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);102 }103 } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {104 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);105 }106 getZero = true;107 }108 // 讓number每次都去掉最后一個(gè)數(shù)109 number = number / 10;110 ++numIndex;111 }112 // 如果signum == -1,則說(shuō)明輸入的數(shù)字為負(fù)數(shù),就在最前面追加特殊字符:負(fù)113 if (signum == -1) {114 sb.insert(0, CN_NEGATIVE);115 }116 // 輸入的數(shù)字小數(shù)點(diǎn)后兩位為"00"的情況,則要在最后追加特殊字符:整117 if (!(scale > 0)) {118 sb.append(CN_FULL);119 }120 return sb.toString();121 }122
123 public static void main(String[] args) {124 double money = 2020004.01;125 BigDecimal numberOfMoney = new BigDecimal(money);126 String s = NumberToCN.number2CNMontrayUnit(numberOfMoney);127 System.out.println("你輸入的金額為:【"+ money +"】 #--# [" +s.toString()+"]");128 }129 }
總結(jié)
以上是生活随笔為你收集整理的java人民币大小写转换函数_java开发_数字转换汉语中人民币的大写_完整版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 财务管理专业写毕业论文有没有什么好的选题
- 下一篇: chrome网页加载慢问题