转载地址:http://www.open-open.com/lib/view/open1376032417901.html 小写转大写 /** *金额大小写转换工具类 */ public class MoneyUtil { /** 大写数字 */ private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; /** 整数部分的单位 */ private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; /** 小数部分的单位 */ private static final String[] DUNIT = { "角", "分", "厘" }; /** * 得到大写金额。 */ public static String toChinese(String str) { str = str.replaceAll(",", "");// 去掉"," String integerStr;// 整数部分数字 String decimalStr;// 小数部分数字 // 初始化:分离整数部分和小数部分 if (str.indexOf(".") > 0) { integerStr = str.substring(0, str.indexOf(".")); decimalStr = str.substring(str.indexOf(".") + 1); } else if (str.indexOf(".") == 0) { integerStr = ""; decimalStr = str.substring(1); } else { integerStr = str; decimalStr = ""; } // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去) if (!integerStr.equals("")) { integerStr = Long.toString(Long.parseLong(integerStr)); if (integerStr.equals("0")) { integerStr = ""; } } // overflow超出处理能力,直接返回 if (integerStr.length() > IUNIT.length) { System.out.println(str + ":超出处理能力"); return str; } int[] integers = toArray(integerStr);// 整数部分数字 boolean isMust5 = isMust5(integerStr);// 设置万单位 int[] decimals = toArray(decimalStr);// 小数部分数字 return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals); } /** * 整数部分和小数部分转换为数组,从高位至低位 */ private static int[] toArray(String number) { int[] array = new int[number.length()]; for (int i = 0; i < number.length(); i++) { array[i] = Integer.parseInt(number.substring(i, i + 1)); } return array; } /** * 得到中文金额的整数部分。 */ private static String getChineseInteger(int[] integers, boolean isMust5) { StringBuffer chineseInteger = new StringBuffer(""); int length = integers.length; for (int i = 0; i < length; i++) { // 0出现在关键位置:1234(万)5678(亿)9012(万)3456(元) // 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元) String key = ""; if (integers[i] == 0) { if ((length - i) == 13)// 万(亿)(必填) key = IUNIT[4]; else if ((length - i) == 9)// 亿(必填) key = IUNIT[8]; else if ((length - i) == 5 && isMust5)// 万(不必填) key = IUNIT[4]; else if ((length - i) == 1)// 元(必填) key = IUNIT[0]; // 0遇非0时补零,不包含最后一位 if ((length - i) > 1 && integers[i + 1] != 0) key += NUMBERS[0]; } chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1])); } return chineseInteger.toString(); } /** * 得到中文金额的小数部分。 */ private static String getChineseDecimal(int[] decimals) { StringBuffer chineseDecimal = new StringBuffer(""); for (int i = 0; i < decimals.length; i++) { // 舍去3位小数之后的 if (i == 3) break; chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i])); } return chineseDecimal.toString(); } /** * 判断第5位数字的单位"万"是否应加。 */ private static boolean isMust5(String integerStr) { int length = integerStr.length(); if (length > 4) { String subInteger = ""; if (length > 8) { // 取得从低位数,第5到第8位的字串 subInteger = integerStr.substring(length - 8, length - 4); } else { subInteger = integerStr.substring(0, length - 4); } return Integer.parseInt(subInteger) > 0; } else { return false; } } // public static void main(String[] args) { // MoneyUtil moneyUtil = new MoneyUtil(); // System.out.println(moneyUtil.toChinese("10001")); // } } 大写转小写 import java.util.HashMap; import java.util.Map; public class CNNMFilter { private static final Character[] CN_NUMERIC = { '一', '二', '三', '四', '五', '六', '七', '八', '九', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '十', '百', '千', '拾', '佰', '仟', '万', '亿', '○', 'O', '零' }; private static Map<Character, Integer> cnNumeric = null; static { cnNumeric = new HashMap<Character, Integer>(40, 0.85f); for (int j = 0; j < 9; j++) cnNumeric.put(CN_NUMERIC[j], j + 1); for (int j = 9; j < 18; j++) cnNumeric.put(CN_NUMERIC[j], j - 8); cnNumeric.put('两', 2); cnNumeric.put('十', 10); cnNumeric.put('拾', 10); cnNumeric.put('百', 100); cnNumeric.put('佰', 100); cnNumeric.put('千', 1000); cnNumeric.put('仟', 1000); cnNumeric.put('万', 10000); cnNumeric.put('亿', 100000000); } public static int isCNNumeric(char c) { Integer i = cnNumeric.get(c); if (i == null) return -1; return i.intValue(); } public static int cnNumericToArabic(String cnn, boolean flag) { cnn = cnn.trim(); if (cnn.length() == 1) return isCNNumeric(cnn.charAt(0)); if (flag) cnn = cnn.replace('佰', '百').replace('仟', '千').replace('拾', '十') .replace('零', ' '); // System.out.println(cnn); int yi = -1, wan = -1, qian = -1, bai = -1, shi = -1; int val = 0; yi = cnn.lastIndexOf('亿'); if (yi > -1) { val += cnNumericToArabic(cnn.substring(0, yi), false) * 100000000; if (yi < cnn.length() - 1) cnn = cnn.substring(yi + 1, cnn.length()); else cnn = ""; if (cnn.length() == 1) { int arbic = isCNNumeric(cnn.charAt(0)); if (arbic <= 10) val += arbic * 10000000; cnn = ""; } } wan = cnn.lastIndexOf('万'); if (wan > -1) { val += cnNumericToArabic(cnn.substring(0, wan), false) * 10000; if (wan < cnn.length() - 1) cnn = cnn.substring(wan + 1, cnn.length()); else cnn = ""; if (cnn.length() == 1) { int arbic = isCNNumeric(cnn.charAt(0)); if (arbic <= 10) val += arbic * 1000; cnn = ""; } } qian = cnn.lastIndexOf('千'); if (qian > -1) { val += cnNumericToArabic(cnn.substring(0, qian), false) * 1000; if (qian < cnn.length() - 1) cnn = cnn.substring(qian + 1, cnn.length()); else cnn = ""; if (cnn.length() == 1) { int arbic = isCNNumeric(cnn.charAt(0)); if (arbic <= 10) val += arbic * 100; cnn = ""; } } bai = cnn.lastIndexOf('百'); if (bai > -1) { val += cnNumericToArabic(cnn.substring(0, bai), false) * 100; if (bai < cnn.length() - 1) cnn = cnn.substring(bai + 1, cnn.length()); else cnn = ""; if (cnn.length() == 1) { int arbic = isCNNumeric(cnn.charAt(0)); if (arbic <= 10) val += arbic * 10; cnn = ""; } } shi = cnn.lastIndexOf('十'); if (shi > -1) { if (shi == 0) val += 1 * 10; else val += cnNumericToArabic(cnn.substring(0, shi), false) * 10; if (shi < cnn.length() - 1) cnn = cnn.substring(shi + 1, cnn.length()); else cnn = ""; } cnn = cnn.trim(); for (int j = 0; j < cnn.length(); j++) val += isCNNumeric(cnn.charAt(j)) * Math.pow(10, cnn.length() - j - 1); return val; } public static int qCNNumericToArabic(String cnn) { int val = 0; cnn = cnn.trim(); for (int j = 0; j < cnn.length(); j++) val += isCNNumeric(cnn.charAt(j)) * Math.pow(10, cnn.length() - j - 1); return val; } public static void main(String[] args) { int val = 0; long s = System.nanoTime(); val = cnNumericToArabic("三亿二千零六万七千五百六", true); System.out.println(val); val = cnNumericToArabic("一九九八", true); System.out.println(val); long e = System.nanoTime(); System.out.format("Done[" + val + "], cost: %.5fsec\n", ((float) (e - s)) / 1E9); } }
相关推荐
可以把金额数值转为财务上常用的金额大写,如100元 转为壹佰园
java代码将金额数字转为中文工具类。 java金额转换工具类。
将阿拉伯数字金额转成中文大写 "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"......
主要为大家详细介绍了java金额数字转中文工具类的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
将金额转换为中文传统形式总结 三种形式 工具类 java代码
D:\002 我的工具类\026 数字金额大写转换\ConvertNumberToUpper.java D:\002 我的工具类\027 Java6实现调用操作平台桌面系统 D:\002 我的工具类\027 Java6实现调用操作平台桌面系统\DesktopDemo.java
{} //下面是lichaohui的工具函数 class function LzhFmtInt(IntValue: Int64; IsYear: Boolean = False; SoftTone: Boolean = False): String; class function LzhFmtDigits(Digits: String): String; class ...
Java中关于金额小写转换为大写的工具类
EhCacheUtils - 基于ehcache的工具类 LruCacheUtils - 基于LinkedHashMap实现LRU缓存的工具类 MemcachedUtils - 基于memcached的工具类 RedisUtils - 基于redis的工具类,与redis的集群配置无缝结合 db JdbcUtils ...
C# 程序辅助工具类 // 程序信息// 程序启动器// (暂无)// 程序单开验证// 权限信息// 开机启动项// 数组格式化// 列表内容判断// 列表内容判断(ListTool)// 字节压缩// 人民币格式化// 中文时辰转换// 日期时间...
将数字金额转换为大写汉字的java工具类,方便财务相关开发转换金额。
对于日期,随机字符,时间差计算,拼音转换,金额计算转换,网络图片保存,身份证出生日期获取,字符串首字母获取,年月加减计算等
有些变态的报表需要把数字转换成英文输出,但愿这个能帮得到你 one hundred eighty-eight thousand, eight hundred one hundred eighty-eight thousand, eight hundred one one hundred eighty-eight thousand, ...
功能揪取:1200元转换为 壹仟贰佰元 AmountInWords(1200,x)= 壹仟贰佰元 其中x为 小数点后四舍五入的位数
Android中数字转中文,金额转大写源码,支持口语化转换。
数字转换为人民币的大写 把输入的金额转换为汉语中人民币的大写
内置了一些工具类,例如手机号脱敏、精确的加减乘除、金额转换等 内置了一些 ts 的编码规范 uniapp+ts 开发小程序的项目最佳实践模板 配置了 vuex,使用模块化,配置了数据持久化 使用 vuex-class 里的装饰器写法...
主要介绍了Java实现数字转成英文的方法,涉及java数组与字符串的相关操作技巧,需要的朋友可以参考下
本工具较同类转换工具准确率达100%。 【背景颜色】 提供可视化的可选择着色方式的背景着色功能,默认启用智能着色。 【文件批量改名】 功能完善的文件或图片批量改名工具。细分为<用数字顺序或倒序><用EXCEL存储格...
Decimal 金额转换,工具类,提供常用的Decimal类型与其它类型之间的转换。由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 * 确的浮点数运算,包括加减乘除和四舍五入。