/** * 阿拉伯数字与中文数字(大小写)之间的转换 * */ 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(); } }
相关推荐
这是一个把人民币数字转换成大写的java程序
小写转换大写人民币,精确到万亿!小写转换大写人民币,精确到万亿!
java钱币小写转换大写
基于java的人民币大写的转换,基本实现了亿以内的小写转大写的功能
主要为大家详细介绍了java实现数字转换人民币中文大写工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java人民币大写转换例子。
Java中关于金额小写转换为大写的工具类
输入字符,通过正则验证是否为金额不超过万亿精确到小数点后两位,然后将数字转换为大写 包括后台java正则验证及前台js验证 请输入一个数字(精确到小数点后两位): fdsa54325.54 fdsa54325.54 false 请输入一个...
数字到人民币大写转换程序(java)支持负数,解决零零,。
人民币数字转换成汉字大写 android Java 运行效率
数字转换成人民币大写
可以把金额数值转为财务上常用的金额大写,如100元 转为壹佰园
手写java类,金额转换成大写,代码有详细说明,金额装换成大写可达百万亿,需要将金额转换成大写的朋友可以下载直接用。
Java人民币金额数字转换成大写,最常见的java面试题,最合理的解答。
数字转换为大写(JAVA) 自动转换 小于5位整数和不大于2位小数
Java语言人民币转化为大写,支持一亿以内的数字;
java中把数字转化为人民币大写形式工具
java代码-使用java输入一个字符串,将字符串中的所有大写字母转换为小写字母,所有小写字母转换为大写字的源代码 ——学习参考资料:仅用于个人学习使用!
java将数字转换成大写字母java将数字转换成大写字母java将数字转换成大写字母java将数字转换成大写字母java将数字转换成大写字母
Java语言实现的,将数字金额转换为人民币大写金额的例子,没有对输入数字进行校验,小数点后只保留两位,支持千分符。最大金额范围为千亿。