java实现数字转换人民币中文大写工具实现数字转换人民币中文大写工具
主要为大家详细介绍了java实现数字转换人民币中文大写工具,具有一定的参考价值,感兴趣的小伙伴们可以参
考一下
本文实例为大家分享了java实现数字转换人民币中文大写的具体代码,供大家参考,具体内容如下
业务场景:业务场景:
1.在人事业务系统开发的报表打印文书时经常需要将人民币数值转换为大写, 所以用java写了一个通用的大写转换函数。 为
了更加方便调用函数的参数使用字符串类型。例如:825.45 转换后:捌佰贰拾伍元肆角伍分
2.在人事业务系统中经常把日期用汉字大写表示,为了方便调用函数,也是使用字符串类型。例如:2018 转换后:二〇一八
下面为java代码
public class Data2Zh {
final static private String NUMBER[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
final static private String NUMBER2[] = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
final static private String CBit[] = { "", "拾", "佰", "仟" };
/**
* 将数值大写
*/
public static String capitalization(String szNum) {
StringBuilder resstr = new StringBuilder();
String tmpstr = szNum.trim();
int sl = tmpstr.length();
int sp = 0;
int dotpos = tmpstr.indexOf('.');
if (dotpos != -1) {
while (sl > 1 && tmpstr.charAt(sl - 1) == '0')
sl--;
if (tmpstr.charAt(sl - 1) == '.')
sl--;
if (sl != tmpstr.length()) {
tmpstr = tmpstr.substring(0, sl);
}
} else
dotpos = sl;
if (sl < 1)
return NUMBER[0];
if (tmpstr.charAt(0) == '-') {
resstr.append("负");
sp = 1;
}
String integerNum = tmpstr.substring(sp, dotpos - sp);
String decimalNum = "";
if (dotpos + 1 < sl)
decimalNum = tmpstr.substring(dotpos + 1);
sl = integerNum.length();
sp = 0;
while (sp < sl && integerNum.charAt(sp) == '0')
sp++;
if (sp > 0)
integerNum = integerNum.substring(sp);
int inl = integerNum.length();
if (inl > 0) {
int h = (inl - 1) % 4;
int j = (inl - 1) / 4 + 1;
sp = 0;
boolean allzero = false;
boolean preallzero = false;
for (; j > 0; j--) {
int k = h;
h = 3;
boolean preiszero = allzero;
allzero = true;
for (; k >= 0; k--, sp++) {
if (integerNum.charAt(sp) == '0')
preiszero = true;
else {
allzero = false;
if (preiszero)
resstr.append("零");
preiszero = false;
resstr.append(NUMBER[(byte) (integerNum.charAt(sp)) - 48]).append(CBit[k]);
}
}
评论0