您好,登錄后才能下訂單哦!
本文實例為大家分享了java數(shù)字轉(zhuǎn)漢字工具類的具體代碼,供大家參考,具體內(nèi)容如下
/** * Created by 33303 on 2017/7/28. */ import java.math.BigDecimal; /** * 數(shù)字轉(zhuǎn)換為漢語中人民幣的大寫<br> * */ public class NumberToCN { /** * 漢語中數(shù)字大寫 */ private static final String[] CN_UPPER_NUMBER = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; /** * 漢語中貨幣單位大寫,這樣的設(shè)計類似于占位符 */ private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟", "兆", "拾", "佰", "仟" }; /** * 特殊字符:整 */ 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)換為漢語中人民幣的大寫 * * @param numberOfMoney * 輸入的金額 * @return 對應(yīng)的漢語大寫 */ 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; } //這里會進(jìn)行金額的四舍五入 long number = numberOfMoney.movePointRight(MONEY_PRECISION) .setScale(0, 4).abs().longValue(); // 得到小數(shù)點后兩位值 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; } // 每次獲取到最后一個數(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每次都去掉最后一個數(shù) number = number / 10; ++numIndex; } // 如果signum == -1,則說明輸入的數(shù)字為負(fù)數(shù),就在最前面追加特殊字符:負(fù) if (signum == -1) { sb.insert(0, CN_NEGATIVE); } // 輸入的數(shù)字小數(shù)點后兩位為"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()+"]"); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。