您好,登錄后才能下訂單哦!
本文實例為大家分享了java實現阿拉伯數字轉漢字數字的具體代碼,供大家參考,具體內容如下
package bin; /** * @author Elsa * @param args * @param 2013-1-21 */ public class DoubleTest { static String[] units = { "", "十", "百", "千", "萬", "十萬", "百萬", "千萬", "億", "十億", "百億", "千億", "萬億" }; static char[] numArray = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' }; /** * 測試 * @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(); } /** * 對于double類型的的裝換 * * @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; } /** * 對整數類型的轉換 * * @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(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。