本文實例為大家分享了java實現阿拉伯數字轉漢字數字的具體代碼,供大家參考,具體內容如下
創(chuàng)新互聯公司是一家專注于成都網站設計、成都網站制作與策劃設計,蒙城網站建設哪家好?創(chuàng)新互聯公司做網站,專注于網站建設10年,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:蒙城等地區(qū)。蒙城做網站價格咨詢:13518219792
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(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯。