string實現(xiàn)數(shù)字轉(zhuǎn)中文的方法

本篇內(nèi)容主要講解“string實現(xiàn)數(shù)字轉(zhuǎn)中文的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“string實現(xiàn)數(shù)字轉(zhuǎn)中文的方法”吧!

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),簡陽企業(yè)網(wǎng)站建設(shè),簡陽品牌網(wǎng)站建設(shè),網(wǎng)站定制,簡陽網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,簡陽網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

public static final String EMPTY = "";

public static final String ZERO = "零";

public static final String ONE = "壹";

public static final String TWO = "貳";

public static final String THREE = "叁";

public static final String FOUR = "肆";

public static final String FIVE = "伍";

public static final String SIX = "陸";

public static final String SEVEN = "柒";

public static final String EIGHT = "捌";

public static final String NINE = "玖";

public static final String TEN = "拾";

public static final String HUNDRED = "佰";

public static final String THOUSAND = "仟";

public static final String TEN_THOUSAND = "萬";

public static final String HUNDRED_MILLION = "億";

public static final String YUAN = "元";

public static final String JIAO = "角";

public static final String FEN = "分";

public static final String DOT = ".";

private static MoneyFormatTest formattest = null;//定義一個靜態(tài)的MoneyFormatTest類讓它空

//建了兩個HashMap數(shù)的散列表。

private HashMap chineseNumberMap = new HashMap();

private HashMap chineseMoneyPattern = new HashMap();

/*NumberFormat 是所有數(shù)值格式的抽象基類。 該類提供了格式化和分析數(shù)值的接口。 NumberFormat 也提供了確定哪個語言環(huán)境具有數(shù)值格式以及它們名字的方法。

NumberFormat 幫助格式化和分析任何語言環(huán)境的數(shù)值。 代碼可以完全不依賴于語言環(huán)境中關(guān)于十進制小數(shù)點、千進位分隔符的約定,甚至關(guān)于使用特別的十進制數(shù)字或數(shù)值格式是否為小數(shù)的約定。

*/

private NumberFormat numberFormat = NumberFormat.getInstance();//為了格式化當前 Locale 的數(shù)值,使用下列靜態(tài)工廠方法之一。

//構(gòu)造方法

private MoneyFormatTest() {

numberFormat.setMaximumFractionDigits(4);

numberFormat.setMinimumFractionDigits(2);

numberFormat.setGroupingUsed(false);

//在該散列表中映射指定的 鍵 到指定的 值 。

chineseNumberMap.put("0", ZERO);

chineseNumberMap.put("1", ONE);

chineseNumberMap.put("2", TWO);

chineseNumberMap.put("3", THREE);

chineseNumberMap.put("4", FOUR);

chineseNumberMap.put("5", FIVE);

chineseNumberMap.put("6", SIX);

chineseNumberMap.put("7", SEVEN);

chineseNumberMap.put("8", EIGHT);

chineseNumberMap.put("9", NINE);

chineseNumberMap.put(DOT, DOT);

chineseMoneyPattern.put("1", TEN);

chineseMoneyPattern.put("2", HUNDRED);

chineseMoneyPattern.put("3", THOUSAND);

chineseMoneyPattern.put("4", TEN_THOUSAND);

chineseMoneyPattern.put("5", TEN);

chineseMoneyPattern.put("6", HUNDRED);

chineseMoneyPattern.put("7", THOUSAND);

chineseMoneyPattern.put("8", HUNDRED_MILLION);

}

//如果是formatter等于空,也就是如果是第一次載入的就實例化SimpleMoneyFormat類,返回SimpleMoneyFormat的一個對象。

public static MoneyFormatTest getInstance() {

if (formattest == null)

formattest= new MoneyFormatTest();

return formattest;

}

//重載format方法格式的規(guī)范傳入不同類型的參數(shù),返回String類型

public String format(String moneyStr) {

checkPrecision(moneyStr);

String result;

result = convertToChineseNumber(moneyStr);

result = addUnitsToChineseMoneyString(result);

return result;

}

public String format(double moneyDouble) {

return format(numberFormat.format(moneyDouble));

}

public String format(int moneyInt) {

return format(numberFormat.format(moneyInt));

}

public String format(long moneyLong) {

return format(numberFormat.format(moneyLong));

}

public String format(Number moneyNum) {

return format(numberFormat.format(moneyNum));

}

private String convertToChineseNumber(String moneyStr) {

String result;

StringBuffer cMoneyStringBuffer = new StringBuffer();//定義字符串緩沖區(qū)實現(xiàn)可變字符序列

for (int i = 0; i < moneyStr.length(); i++) {

cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));//添加字符串到字符串緩沖區(qū)。

}

//拾佰仟萬億等都是漢字里面才有的單位,加上它們

int indexOfDot = cMoneyStringBuffer.indexOf(DOT);

int moneyPatternCursor = 1;

for (int i = indexOfDot - 1; i > 0; i--) {

cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));

moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1;

}

到此,相信大家對“string實現(xiàn)數(shù)字轉(zhuǎn)中文的方法”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

新聞標題:string實現(xiàn)數(shù)字轉(zhuǎn)中文的方法
文章鏈接:http://www.muchs.cn/article28/ijcpcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)站建設(shè)、品牌網(wǎng)站制作Google、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司