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;
}
}
}
分享到:
相关推荐
使用java实现对人民币大写处理,亲测可用!
这是一个把人民币数字转换成大写的java程序
基于java的人民币大写的转换,基本实现了亿以内的小写转大写的功能
数字到人民币大写转换程序(java)支持负数,解决零零,。
主要为大家详细介绍了java实现数字转换人民币中文大写工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java语言人民币转化为大写,支持一亿以内的数字;
java人民币大写转换例子。
人民币数字转大写.java
将人民币转换为大写,实现了仟亿级别以内的大写转换。 具体内容看博客 https://blog.csdn.net/H200102/article/details/104672484
java中把数字转化为人民币大写形式工具
本文实例为大家分享了python实现人民币大写转换的具体代码,供大家参考,具体内容如下 #!/usr/bin/python # -*- coding:utf-8 -*- # ********* 转换方法介绍 ********* # 将需要转换的数字从右向左,每4位分成一个...
用java写的数字转人民币大写,在下java入门不久,望大家多多指点
数字转换成人民币大写
设计语言:java 这里给出源代码 例如: 123.45 转化为:壹佰貳拾叁元肆角伍分 123456 转化为:壹拾貳万叁仟肆佰伍拾陆元整
人民币大写转换人民币大写转换人民币大写转换人民币大写转换人民币大写转换
可以把金额数值转为财务上常用的金额大写,如100元 转为壹佰园
java钱币小写转换大写
电子表格中轻松输入人民币大写,只需输入小写金额,自动出现人民币大写。如果需要,只需把大写金额框中的公式复制自己的目标位置就可,(适当修改一下单元格名称)
将阿拉伯数字转换成大写人民币汉字 大写人民币
1. 最大支持百万亿(15位整数,2位小数) ;或仟万亿(16位整数) 2. 超过最大支持仟万亿(16位整数),只输出大写数字 3. 整数部分越大,则小数部分输出越不精确 4. 负数,则忽略负号