浏览 3586 次
锁定老帖子 主题:将阿拉伯数字转换成中文汉字格式
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-22
import java.text.*; /** * @author wang wei wKF22287 * @since 2010 */ public class CNNumberTools { /** "零","壹","贰","叁","肆","伍","陆","柒","捌","玖" */ private final static String[] nums = new String[] { "\u96f6", "\u58f9", "\u8d30", "\u53c1", "\u8086", "\u4f0d", "\u9646", "\u67d2", "\u634c", "\u7396" }; /** "拾","佰","仟","万","亿" */ private final static String[] units = new String[] { "\u62fe", "\u4f70", "\u4edf", "\u4e07", "\u4ebf" }; private final static NumberFormat numberFormat = new DecimalFormat( "#,####.#"); public static String convertToCNNumber(double number) { if (number > Double.MAX_VALUE || number < 0.0) { return null; } String srcNum = numberFormat.format(number); //按个数转换,如"21,1234,4567,5487.4543544" String prefixNum = srcNum; if (srcNum.indexOf(".") != -1) { prefixNum = srcNum.substring(0, srcNum.indexOf(".")); //小数点前 } StringBuffer result = new StringBuffer(0); //用于保存结果 String[] numPices = prefixNum.split(","); //4个数字一组 for (int i = 0; i < numPices.length; i++) //遍历每个组 { for (int j = 0; j < numPices[i].length(); j++) //遍历组中的每个数字 { int k = Integer.parseInt(String.valueOf(numPices[i].charAt(j))); int len = numPices[i].length(); result.append(nums[k]); //变成汉字 result.append(len - 2 - j >= 0 && k > 0 ? units[len - 2 - j] : ""); //添加仟佰拾 result .append(j != len - 1 ? "" : ((i + numPices.length) % 2 == 0 ? (i == numPices.length - 1 ? "" : units[3]) : i != numPices.length - 1 ? units[4] : "")); //添加亿万 } } String resutlStr = result.toString(); resutlStr = resutlStr.replaceAll(nums[0] + "{2,}", nums[0]); //"零零" to "零" resutlStr = resutlStr.replaceAll(nums[0] + units[3] + "{1}", units[3]); //"零万" to "万" resutlStr = resutlStr.replaceAll(nums[0] + units[4] + "{1}", units[4]); //"零亿" to "亿" resutlStr = resutlStr.replaceAll(units[4] + units[3] + "{1}", units[4]); //"亿万" to "亿零" if (resutlStr.lastIndexOf(nums[0]) == resutlStr.length() - 1) { //去掉最后的"零" resutlStr = resutlStr.substring(0, resutlStr.length() - 1); } return resutlStr; } public static void main(String[] args) { String a = CNNumberTools.convertToCNNumber(Long.MAX_VALUE); System.out.println(Long.MAX_VALUE); System.out.println(a); // 9223372036854775807 // 玖佰贰拾贰亿叁仟叁佰柒拾贰万零叁佰陆拾捌亿伍仟肆佰柒拾柒万陆仟 } } 一个考试题目,没写全,欢迎指点 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-05-03
呵呵,我写过一个双色球的生成器,后来听说双色球有幕后操控,就改成双色球生成器娱乐版了!
|
|
返回顶楼 | |