java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具
本文實(shí)例為大家分享了java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫的具體代碼,供大家參考,具體內(nèi)容如下
業(yè)務(wù)場景:
1.在人事業(yè)務(wù)系統(tǒng)開發(fā)的報(bào)表打印文書時(shí)經(jīng)常需要將人民幣數(shù)值轉(zhuǎn)換為大寫, 所以用java寫了一個(gè)通用的大寫轉(zhuǎn)換函數(shù)。 為了更加方便調(diào)用函數(shù)的參數(shù)使用字符串類型。例如:825.45 轉(zhuǎn)換后:捌佰貳拾伍元肆角伍分
2.在人事業(yè)務(wù)系統(tǒng)中經(jīng)常把日期用漢字大寫表示,為了方便調(diào)用函數(shù),也是使用字符串類型。例如:2018 轉(zhuǎn)換后:二〇一八
下面為java代碼
public class Data2Zh { final static private String NUMBER[] = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; final static private String NUMBER2[] = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; final static private String CBit[] = { "", "拾", "佰", "仟" }; /** * 將數(shù)值大寫 */ public static String capitalization(String szNum) { StringBuilder resstr = new StringBuilder(); String tmpstr = szNum.trim(); int sl = tmpstr.length(); int sp = 0; int dotpos = tmpstr.indexOf('.'); if (dotpos != -1) { while (sl > 1 && tmpstr.charAt(sl - 1) == '0') sl--; if (tmpstr.charAt(sl - 1) == '.') sl--; if (sl != tmpstr.length()) { tmpstr = tmpstr.substring(0, sl); } } else dotpos = sl; if (sl < 1) return NUMBER[0]; if (tmpstr.charAt(0) == '-') { resstr.append("負(fù)"); sp = 1; } String integerNum = tmpstr.substring(sp, dotpos - sp); String decimalNum = ""; if (dotpos + 1 < sl) decimalNum = tmpstr.substring(dotpos + 1); sl = integerNum.length(); sp = 0; while (sp < sl && integerNum.charAt(sp) == '0') sp++; if (sp > 0) integerNum = integerNum.substring(sp); int inl = integerNum.length(); if (inl > 0) { int h = (inl - 1) % 4; int j = (inl - 1) / 4 + 1; sp = 0; boolean allzero = false; boolean preallzero = false; for (; j > 0; j--) { int k = h; h = 3; boolean preiszero = allzero; allzero = true; for (; k >= 0; k--, sp++) { if (integerNum.charAt(sp) == '0') preiszero = true; else { allzero = false; if (preiszero) resstr.append("零"); preiszero = false; resstr.append(NUMBER[(byte) (integerNum.charAt(sp)) - 48]).append(CBit[k]); } } // end for k if (/* j!=0 && */ j % 2 == 0) { if (!allzero) resstr.append("萬"); } else { if (!allzero || !preallzero) { int repyi = j / 2; for (int i = 0; i < repyi; i++) resstr.append("億"); } } preallzero = allzero; } // end for j } else resstr.append("零"); int dnl = decimalNum.length(); if (dnl > 0) { resstr.append("點(diǎn)"); for (int i = 0; i < dnl; i++) { resstr.append(NUMBER[(byte) (decimalNum.charAt(i)) - 48]); } } return resstr.toString(); } /** * 獲得某一位上的數(shù)值,如果 nBit<0 則獲得小數(shù)點(diǎn)后面的位數(shù) */ static public char getNumByte(String szNum, int nBit) { int sl = szNum.length(); int nPos = 0; while (nPos < sl && szNum.charAt(nPos) != '.') nPos++; if (nBit < 0) nPos = nPos - nBit; else nPos = nPos - nBit - 1; if (nPos < 0 || nPos >= sl) return '0'; return szNum.charAt(nPos); } public static String rmbChange(String rmb) { return capitalization((rmb.indexOf('.') >= 0 ? rmb.substring(0, rmb.indexOf('.')) : rmb)) + "元" + capitalization(String.valueOf(getNumByte(rmb, -1))) + "角" + capitalization(String.valueOf(getNumByte(rmb, -2))) + "分"; } /** * 僅僅是把 0~9 轉(zhuǎn)換為 "〇","一","二","三","四","五","六","七","八","九" */ public static String changeCN(String szNum) { StringBuilder sb = new StringBuilder(); String str = szNum.trim(); int sl = str.length(); int sp = 0; if (sl < 1) return NUMBER2[0]; for (; sp < sl; sp++) if (str.charAt(sp) >= '0' && str.charAt(sp) <= '9') sb.append(NUMBER2[str.charAt(sp) - '0']); else sb.append(str.charAt(sp)); return sb.toString(); } public static void main(String[] args) { System.out.println(rmbChange("825.45")); System.out.println(changeCN("2018")); } }
效果圖:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中hasNextInt判斷后無限循環(huán)輸出else項(xiàng)的解決方法
這篇文章主要介紹了java中hasNextInt判斷后無限循環(huán)輸出else項(xiàng)的解決方法的相關(guān)資料,需要的朋友可以參考下2016-10-10java智能問答圖靈機(jī)器人AI接口(聚合數(shù)據(jù))
這篇文章主要介紹了java智能問答圖靈機(jī)器人AI接口(聚合數(shù)據(jù)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02springboot獲取URL請(qǐng)求參數(shù)的多種方式
這篇文章主要介紹了springboot獲取URL請(qǐng)求參數(shù)的多種方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國內(nèi)又以Mybatis用的多,基于mybatis上的增強(qiáng)框架,又有mybatis plus和TK mybatis等。今天我們介紹一個(gè)新的mybatis增強(qiáng)框架 fluent mybatis關(guān)于聚合查詢、apply方法詳解2021-10-10Java靜態(tài)內(nèi)部類實(shí)現(xiàn)單例過程
這篇文章主要介紹了Java靜態(tài)內(nèi)部類實(shí)現(xiàn)單例過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10