/** * 阿拉伯数字与中文数字(大小写)之间的转换 * */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * @author * */ public final class SwitchMethod { private static final String[] pattern = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; private static final String[] cPattern = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; private static final String ZEOR = "零"; /** * 参数名称:@param date 待转换的日期(Date格式) * 返回值:String 小写汉字的日期 * 方法描述:土办法解决日期转换问题 * @author:HY * 创建时间:2011-6-20 下午05:59:20 */ public static void dateToChineseSmall(Date date) { DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = formatter.format(date); System.out.println(stringToChineseSmall(dateStr)); //return stringToChineseSmall(dateStr); } /** * 参数名称:@param dateStr 待转换的日期(String格式,结构为yyyy-MM-dd) * 返回值:String 小写汉字的日期 * 方法描述:土办法解决日期转换问题 * @author:HY * 创建时间:2011-6-20 下午05:59:20 */ public static String stringToChineseSmall(String dateStr) { // 传参数的格式为 2011-11-26 String resultStr = ""; String[] ss = dateStr.split("-"); for (int j = 0; j < ss[0].length(); j++) { switch (ss[0].charAt(j)) { case '0': resultStr += "〇"; break; case '1': resultStr += "一"; break; case '2': resultStr += "二"; break; case '3': resultStr += "三"; break; case '4': resultStr += "四"; break; case '5': resultStr += "五"; break; case '6': resultStr += "六"; break; case '7': resultStr += "七"; break; case '8': resultStr += "八"; break; case '9': resultStr += "九"; break; } } resultStr = resultStr + "年"; if (ss[1].equals("01")) { resultStr += "一"; } else if (ss[1].equals("02")) { resultStr += "二"; } else if (ss[1].equals("03")) { resultStr += "三"; } else if (ss[1].equals("04")) { resultStr += "四"; } else if (ss[1].equals("05")) { resultStr += "五"; } else if (ss[1].equals("06")) { resultStr += "六"; } else if (ss[1].equals("07")) { resultStr += "七"; } else if (ss[1].equals("08")) { resultStr += "八"; } else if (ss[1].equals("09")) { resultStr += "九"; } else if (ss[1].equals("10")) { resultStr += "十"; } else if (ss[1].equals("11")) { resultStr += "十一"; } else { resultStr += "十二"; } resultStr += "月"; if (ss[2].equals("01")) { resultStr += "一"; } else if (ss[2].equals("02")) { resultStr += "二"; } else if (ss[2].equals("03")) { resultStr += "三"; } else if (ss[2].equals("04")) { resultStr += "四"; } else if (ss[2].equals("05")) { resultStr += "五"; } else if (ss[2].equals("06")) { resultStr += "六"; } else if (ss[2].equals("07")) { resultStr += "七"; } else if (ss[2].equals("08")) { resultStr += "八"; } else if (ss[2].equals("09")) { resultStr += "九"; } else if (ss[2].equals("10")) { resultStr += "十"; } else if (ss[2].equals("11")) { resultStr += "十一"; } else if (ss[2].equals("12")) { resultStr += "十二"; } else if (ss[2].equals("13")) { resultStr += "十三"; } else if (ss[2].equals("14")) { resultStr += "十四"; } else if (ss[2].equals("15")) { resultStr += "十五"; } else if (ss[2].equals("16")) { resultStr += "十六"; } else if (ss[2].equals("17")) { resultStr += "十七"; } else if (ss[2].equals("18")) { resultStr += "十八"; } else if (ss[2].equals("19")) { resultStr += "十九"; } else if (ss[2].equals("20")) { resultStr += "二十"; } else if (ss[2].equals("21")) { resultStr += "二十一"; } else if (ss[2].equals("22")) { resultStr += "二十二"; } else if (ss[2].equals("23")) { resultStr += "二十三"; } else if (ss[2].equals("24")) { resultStr += "二十四"; } else if (ss[2].equals("25")) { resultStr += "二十五"; } else if (ss[2].equals("26")) { resultStr += "二十六"; } else if (ss[2].equals("27")) { resultStr += "二十七"; } else if (ss[2].equals("28")) { resultStr += "二十八"; } else if (ss[2].equals("29")) { resultStr += "二十九"; } else if (ss[2].equals("30")) { resultStr += "三十"; } else if (ss[2].equals("31")) { resultStr += "三十一"; } resultStr += "日"; System.out.println(resultStr); return resultStr; } /** * * @param moneyString * @return */ public static String format(String moneyString) { int dotPoint = moneyString.indexOf("."); // 判断是否为小数 String moneyStr; // 整数部分 if (dotPoint != -1) { moneyStr = moneyString.substring(0, moneyString.indexOf(".")); } else { moneyStr = moneyString; } moneyStr = String.valueOf(Integer.valueOf(moneyStr)) ; StringBuffer ms = new StringBuffer(); for (int i = 0; i < moneyStr.length(); i++) { ms.append(pattern[moneyStr.charAt(i) - 48]); // 按数组的编号加入对应大写汉字 } int cpCursor = 1; for (int j = moneyStr.length() - 1; j > 0; j--) { // 在j之后加字符,不影响j对原字符串的相对位置 ,只是moneyStr.length()不断增加 // ,insert(j,"string")就在j位置处插入,j=0时为第一位 ms.insert(j, cPattern[cpCursor]); cpCursor = cpCursor == 8 ? 1 : cpCursor + 1; // 亿位之后重新循环 } while (ms.indexOf("零拾") != -1) { // 当十位为零时用一个"零"代替"零拾" // replace的起始于终止位置 ms.replace(ms.indexOf("零拾"), ms.indexOf("零拾") + 2, ZEOR); } while (ms.indexOf("零佰") != -1) { // 当百位为零时,同理 ms.replace(ms.indexOf("零佰"), ms.indexOf("零佰") + 2, ZEOR); } while (ms.indexOf("零仟") != -1) { // 同理 ms.replace(ms.indexOf("零仟"), ms.indexOf("零仟") + 2, ZEOR); } while (ms.indexOf("零万") != -1) { // 万需保留,中文习惯 ms.replace(ms.indexOf("零万"), ms.indexOf("零万") + 2, "万"); } while (ms.indexOf("零亿") != -1) { // 同上 ms.replace(ms.indexOf("零亿"), ms.indexOf("零亿") + 2, "亿"); } while (ms.indexOf("零零") != -1) {// 有连续数位出现零,即有以下情况,此时根据习惯保留一个零即可 ms.replace(ms.indexOf("零零"), ms.indexOf("零零") + 2, ZEOR); } while (ms.indexOf("亿万") != -1) { // 特殊情况,如:100000000,根据习惯保留高位 ms.replace(ms.indexOf("亿万"), ms.indexOf("亿万") + 2, "亿"); } if(ms.length()>1){ while (ms.lastIndexOf("零") == ms.length() - 1) { // 当结尾为零j,不必显示,经过处理也只可能出现一个零 ms.delete(ms.lastIndexOf("零"), ms.lastIndexOf("零") + 1); } } System.out.println(); return ms.toString(); } public static void main(String[] args) { //dateToChineseSmall(new Date()); stringToChineseSmall("2012-09-23"); format("1987.45"); } }
写道
下面是是针对带小数点数据的转换
import java.awt.*; import javax.swing.*; public class MyMoney { public static void main(String args[]){ double number; number = Double.parseDouble("3334.67"); String chinese= convertToChineseNumber(number); System.out.println(chinese); } public static String convertToChineseNumber(double number){ StringBuffer chineseNumber = new StringBuffer(); String [] num={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; String [] unit = {"分","角","圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万"}; String tempNumber = String.valueOf(Math.round((number * 100))); int tempNumberLength = tempNumber.length(); if ("0".equals(tempNumber)) { return "零圆整"; } if (tempNumberLength > 15) { try { throw new Exception("超出转化范围."); } catch (Exception e) { e.printStackTrace(); } } boolean preReadZero = true; //前面的字符是否读零 for (int i=tempNumberLength; i>0; i--){ if ((tempNumberLength - i + 2) % 4 == 0) { //如果在(圆,万,亿,万)位上的四个数都为零,如果标志为未读零,则只读零,如果标志为已读零,则略过这四位 if (i - 4 >= 0 && "0000".equals(tempNumber.substring(i - 4, i))) { if (!preReadZero) { chineseNumber.insert(0, "零"); preReadZero = true; } i -= 3; //下面还有一个i-- continue; } //如果当前位在(圆,万,亿,万)位上,则设置标志为已读零(即重置读零标志) preReadZero = true; } int digit = Integer.parseInt(tempNumber.substring(i - 1, i)); if (digit == 0) { //如果当前位是零并且标志为未读零,则读零,并设置标志为已读零 if (!preReadZero) { chineseNumber.insert(0, "零"); preReadZero = true; } //如果当前位是零并且在(圆,万,亿,万)位上,则读出(圆,万,亿,万) if ((tempNumberLength - i + 2) % 4 == 0) { chineseNumber.insert(0, unit[tempNumberLength - i]); } } //如果当前位不为零,则读出此位,并且设置标志为未读零 else { chineseNumber.insert(0, num[digit] + unit[tempNumberLength - i]); preReadZero = false; } } //如果分角两位上的值都为零,则添加一个“整”字 if (tempNumberLength - 2 >= 0 && "00".equals(tempNumber.substring(tempNumberLength - 2, tempNumberLength))) { chineseNumber.append("整"); } return chineseNumber.toString(); } }