java數(shù)字轉(zhuǎn)漢字工具類詳解
更新時(shí)間:2019年04月21日 15:29:38 作者:Tlimited
這篇文章主要為大家詳細(xì)介紹了java數(shù)字轉(zhuǎn)漢字工具類的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java數(shù)字轉(zhuǎn)漢字工具類的具體代碼,供大家參考,具體內(nèi)容如下
/** * Created by 33303 on 2017/7/28. */ import java.math.BigDecimal; /** * 數(shù)字轉(zhuǎn)換為漢語(yǔ)中人民幣的大寫<br> * */ public class NumberToCN { /** * 漢語(yǔ)中數(shù)字大寫 */ private static final String[] CN_UPPER_NUMBER = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; /** * 漢語(yǔ)中貨幣單位大寫,這樣的設(shè)計(jì)類似于占位符 */ private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "萬(wàn)", "拾", "佰", "仟", "億", "拾", "佰", "仟", "兆", "拾", "佰", "仟" }; /** * 特殊字符:整 */ private static final String CN_FULL = "整"; /** * 特殊字符:負(fù) */ private static final String CN_NEGATIVE = "負(fù)"; /** * 金額的精度,默認(rèn)值為2 */ private static final int MONEY_PRECISION = 2; /** * 特殊字符:零元整 */ private static final String CN_ZEOR_FULL = "零元" + CN_FULL; /** * 把輸入的金額轉(zhuǎn)換為漢語(yǔ)中人民幣的大寫 * * @param numberOfMoney * 輸入的金額 * @return 對(duì)應(yīng)的漢語(yǔ)大寫 */ 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; } //這里會(huì)進(jìn)行金額的四舍五入 long number = numberOfMoney.movePointRight(MONEY_PRECISION) .setScale(0, 4).abs().longValue(); // 得到小數(shù)點(diǎn)后兩位值 long scale = number % 100; int numUnit = 0; int numIndex = 0; boolean getZero = false; // 判斷最后兩位數(shù),一共有四中情況: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; } // 每次獲取到最后一個(gè)數(shù) 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每次都去掉最后一個(gè)數(shù) number = number / 10; ++numIndex; } // 如果signum == -1,則說(shuō)明輸入的數(shù)字為負(fù)數(shù),就在最前面追加特殊字符:負(fù) if (signum == -1) { sb.insert(0, CN_NEGATIVE); } // 輸入的數(shù)字小數(shù)點(diǎn)后兩位為"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()+"]"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類
Spring為大家內(nèi)置了不少開(kāi)箱即用的轉(zhuǎn)換類,如字符串轉(zhuǎn)數(shù)字、字符串轉(zhuǎn)時(shí)間等,但有時(shí)候需要使用自定義的屬性,則需要自定義轉(zhuǎn)換類了2021-06-06druid ParserException類錯(cuò)誤問(wèn)題及解決
這篇文章主要介紹了druid ParserException類錯(cuò)誤問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java Web項(xiàng)目中實(shí)現(xiàn)文件下載功能的實(shí)例教程
這篇文章主要介紹了Java Web項(xiàng)目中實(shí)現(xiàn)文件下載功能的實(shí)例教程,分別講解了通過(guò)超鏈接實(shí)現(xiàn)下載以及通過(guò)Servlet程序?qū)崿F(xiàn)下載的方式,需要的朋友可以參考下2016-05-05Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例
這篇文章主要介紹了Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12