您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了java實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)漢字?jǐn)?shù)字的具體代碼,供大家參考,具體內(nèi)容如下
package bin; /** * @author Elsa * @param args * @param 2013-1-21 */ public class DoubleTest { static String[] units = { "", "十", "百", "千", "萬(wàn)", "十萬(wàn)", "百萬(wàn)", "千萬(wàn)", "億", "十億", "百億", "千億", "萬(wàn)億" }; static char[] numArray = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' }; /** * 測(cè)試 * @param args */ public static void main(String[] args) { int num = 234234324; String numStr = foematInteger(num); System.out.println("num= " + num + ", convert result: " + numStr); double decimal = 12312; System.out .println("============================================================"); String decStr = formatDecimal(decimal); System.out.println("decimal= " + decimal + ", decStr: " + decStr); } private static String foematInteger(int num) { char[] val = String.valueOf(num).toCharArray(); int len = val.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { String m = val[i] + ""; int n = Integer.valueOf(m); boolean isZero = n == 0; String unit = units[(len - 1) - i]; if (isZero) { if ('0' == val[i - 1]) { continue; } else { sb.append(numArray[n]); } } else { sb.append(numArray[n]); sb.append(unit); } } return sb.toString(); } /** * 對(duì)于double類(lèi)型的的裝換 * * @param decimal * @return */ private static String formatDecimal(double decimal) { String decimals = String.valueOf(decimal); int decIndex = decimals.indexOf("."); int integ = Integer.valueOf(decimals.substring(0, decIndex)); int dec = Integer.valueOf(decimals.substring(decIndex + 1)); String result = foematInteger(integ) + "." + formatFractionalPart(dec); return result; } /** * 對(duì)整數(shù)類(lèi)型的轉(zhuǎn)換 * * @param decimal * @return */ private static String formatFractionalPart(int decimal) { char[] val = String.valueOf(decimal).toCharArray(); int len = val.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { int n = Integer.valueOf(val[i] + ""); sb.append(numArray[n]); } return sb.toString(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。