java实现数字转换人民币中文大写工具
作者:灵魂_漫步 时间:2023-08-16 08:48:41
本文实例为大家分享了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]);
}
}
// end for k
if (/* j!=0 && */ j % 2 == 0) {
if (!allzero)
resstr.append("万");
} else {
if (!allzero || !preallzero) {
int repyi = j / 2;
for (int i = 0; i < repyi; i++)
resstr.append("亿");
}
}
preallzero = allzero;
}
// end for j
} else
resstr.append("零");
int dnl = decimalNum.length();
if (dnl > 0) {
resstr.append("点");
for (int i = 0; i < dnl; i++) {
resstr.append(NUMBER[(byte) (decimalNum.charAt(i)) - 48]);
}
}
return resstr.toString();
}
/**
* 获得某一位上的数值,如果 nBit<0 则获得小数点后面的位数
*/
static public char getNumByte(String szNum, int nBit) {
int sl = szNum.length();
int nPos = 0;
while (nPos < sl && szNum.charAt(nPos) != '.')
nPos++;
if (nBit < 0)
nPos = nPos - nBit;
else
nPos = nPos - nBit - 1;
if (nPos < 0 || nPos >= sl)
return '0';
return szNum.charAt(nPos);
}
public static String rmbChange(String rmb) {
return capitalization((rmb.indexOf('.') >= 0 ? rmb.substring(0, rmb.indexOf('.')) : rmb)) + "元"
+ capitalization(String.valueOf(getNumByte(rmb, -1))) + "角"
+ capitalization(String.valueOf(getNumByte(rmb, -2))) + "分";
}
/**
* 仅仅是把 0~9 转换为 "〇","一","二","三","四","五","六","七","八","九"
*/
public static String changeCN(String szNum) {
StringBuilder sb = new StringBuilder();
String str = szNum.trim();
int sl = str.length();
int sp = 0;
if (sl < 1)
return NUMBER2[0];
for (; sp < sl; sp++)
if (str.charAt(sp) >= '0' && str.charAt(sp) <= '9')
sb.append(NUMBER2[str.charAt(sp) - '0']);
else
sb.append(str.charAt(sp));
return sb.toString();
}
public static void main(String[] args) {
System.out.println(rmbChange("825.45"));
System.out.println(changeCN("2018"));
}
}
效果图:
来源:https://blog.csdn.net/qq_34417749/article/details/82147513
标签:java,数字,中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
轻松学习C#的属性
2022-07-21 06:24:04
![](https://img.aspxhome.com/file/2023/5/110175_0s.png)
结合.net框架在C#派生类中触发基类事件及实现接口事件
2022-02-23 20:39:57
C#隐藏手机号、邮箱等敏感信息的实现方法
2023-12-08 17:17:45
![](https://img.aspxhome.com/file/2023/3/125973_0s.png)
Android自定义View接收输入法输入的内容
2022-08-28 12:25:42
Android编程自定义菜单实现方法详解
2022-08-25 18:16:08
![](https://img.aspxhome.com/file/2023/2/137482_0s.jpg)
Java使用OpenCV3.2实现视频读取与播放
2023-11-23 06:14:02
![](https://img.aspxhome.com/file/2023/5/82425_0s.jpg)
Android编程计算函数时间戳的相关方法总结
2023-05-27 18:29:57
聊聊SpringMVC项目依赖和静态资源导出问题
2023-03-26 13:32:20
java.lang.ExceptionInInitializerError异常的解决方法
2023-01-13 04:23:16
C#中List〈string〉和string[]数组之间的相互转换
2023-07-11 22:33:27
![](https://img.aspxhome.com/file/2023/2/66122_0s.jpg)
Java Kafka分区发送及消费实战
2022-08-17 20:27:58
java实现统计字符串中字符及子字符串个数的方法示例
2022-10-14 13:47:40
![](https://img.aspxhome.com/file/2023/2/129102_0s.png)
C# WinForm国际化实现的简单方法
2023-08-07 04:29:34
![](https://img.aspxhome.com/file/2023/2/92902_0s.jpg)
Kotlin浅析延迟初始化与密封类的实现方法
2023-05-06 13:54:44
C#值类型和引用类型的深入理解
2021-07-13 07:45:11
![](https://img.aspxhome.com/file/2023/9/98279_0s.png)
Spring Boot使用profile如何配置不同环境的配置文件
2023-11-25 12:52:36
![](https://img.aspxhome.com/file/2023/2/59932_0s.png)
Android 添加TextView删除线(代码简单)
2022-05-27 16:48:47
![](https://img.aspxhome.com/file/2023/3/109213_0s.png)
深入了解Java中String、Char和Int之间的相互转换
2022-09-14 10:18:54
![](https://img.aspxhome.com/file/2023/2/64382_0s.jpg)
JAVA实现红包分发的示例代码
2022-10-08 06:18:15
![](https://img.aspxhome.com/file/2023/0/66640_0s.png)
ibatis学习之搭建Java项目
2021-11-02 00:35:29