Java人民币小写转大写字符串的实现
作者:丶安格拉丶 时间:2021-10-24 00:52:34
写了一个人民币小写转大写的方法,Java版本,思路很简单,没有测出什么Bug,有bug欢迎反馈
public class RMBChange {
private static String[] nums = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
// 单位还可以往上
private static String[] unit = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
// 小数位如还有需求可以直接往这个数组里加,
private static String[] countNum = {"角", "分"};
public static String change(String money) {
// 过滤空
if (money == null || "".equals(money)) {
return "";
}
StringBuilder res = new StringBuilder();
String[] splitStr = money.split("\\.");
if (splitStr.length > 2) {
throw new RuntimeException("输入的参数不是数字!");
}
String front = splitStr[0];
// 用于判定0的显示
boolean isZero = true;
if (front.length() > unit.length) {
throw new RuntimeException("输入的参数大于万亿!");
}
for (int i = 0; i < front.length(); i++) {
// 整数位处理
int dw = front.length() - i - 1;
// 用ASCII码获得数字
int index = (front.charAt(i) - '0');
if (index == 0) {
isZero = true;
if (dw == 0 || dw == 4 || dw == 8) {
// 元,万,亿需要拼接
res.append(unit[dw]);
}
} else {
if (isZero && i != 0) {
// 多个0只显示一个
res.append(nums[0]);
}
isZero = false;
res.append(nums[index]);
res.append(unit[dw]);
}
}
// 判断是否有小数位
if (splitStr.length > 1) {
// 小数位处理,如果不需要则直接删除这个if及内部所有内容
isZero = true;
String back = splitStr[1];
if (back.length() > countNum.length) {
throw new RuntimeException("小数位小于【" + countNum[countNum.length - 1] + "】!");
}
for (int i = 0; i < back.length(); i++) {
int index = (back.charAt(i) - '0');
if (index == 0) {
isZero = true;
} else {
if (isZero && i != 0) {
res.append(nums[0]);
}
isZero = false;
res.append(nums[index]);
res.append(countNum[i]);
}
}
}
return res.toString();
}
}
测试数据
public static void main(String[] args) {
System.out.println(change("9999999999999.99")); // 玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖元玖角玖分
System.out.println(change("5020055606500.00")); // 伍万零贰佰亿零伍仟伍佰陆拾万零陆仟伍佰元
System.out.println(change("4001500050.16")); // 肆拾亿零壹佰伍拾万零伍拾元壹角陆分
System.out.println(change("7854416")); // 柒佰捌拾伍万肆仟肆佰壹拾陆元
System.out.println(change("9556")); // 玖仟伍佰伍拾陆元
System.out.println(change("10086.0")); // 壹万零捌拾陆元
System.out.println(change("263341.26")); // 贰拾陆万叁仟叁佰肆拾壹元贰角陆分
System.out.println(change("3651.06")); // 叁仟陆佰伍拾壹元零陆分
System.out.println(change("1.00")); // 壹元
System.out.println(change("300.00")); // 叁佰元
System.out.println(change("2007.01")); // 贰仟零柒元零壹分
}
输出结果
玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖元玖角玖分
伍万零贰佰亿零伍仟伍佰陆拾万零陆仟伍佰元
肆拾亿零壹佰伍拾万零伍拾元壹角陆分
柒佰捌拾伍万肆仟肆佰壹拾陆元
玖仟伍佰伍拾陆元
壹万零捌拾陆元
贰拾陆万叁仟叁佰肆拾壹元贰角陆分
叁仟陆佰伍拾壹元零陆分
壹元
叁佰元
贰仟零柒元零壹分
来源:https://blog.csdn.net/qq_39579105/article/details/115761313
标签:Java,人民币,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot定时任务动态扩展ScheduledTaskRegistrar详解
2023-04-23 08:44:06
![](https://img.aspxhome.com/file/2023/8/62678_0s.jpg)
通过springboot+mybatis+druid配置动态数据源
2023-06-20 16:48:30
![](https://img.aspxhome.com/file/2023/8/77438_0s.jpg)
Java 遍历取出Map集合key-value数据的4种方法
2022-02-03 02:48:59
![](https://img.aspxhome.com/file/2023/0/71640_0s.png)
spring boot 实现配置多个DispatcherServlet最简单方式
2023-12-18 03:24:46
![](https://img.aspxhome.com/file/2023/9/63769_0s.jpg)
Spring Boot 2.7.6整合redis与低版本的区别
2023-11-11 19:44:56
![](https://img.aspxhome.com/file/2023/7/59697_0s.png)
使用eclipse创建java项目的方法
2022-10-15 04:41:00
![](https://img.aspxhome.com/file/2023/0/64010_0s.jpg)
如何用.NETCore操作RabbitMQ
2022-06-20 04:11:04
![](https://img.aspxhome.com/file/2023/8/76378_0s.png)
教你创建一个带诊断工具的.NET镜像
2021-09-28 03:11:11
![](https://img.aspxhome.com/file/2023/2/82152_0s.png)
Maven install 报错"程序包不存在"问题的解决方法
2021-05-27 06:08:06
![](https://img.aspxhome.com/file/2023/1/76621_0s.png)
浅谈MyBatis中@MapKey的妙用
2023-09-25 10:42:02
![](https://img.aspxhome.com/file/2023/7/81887_0s.png)
c# wpf如何使用Blend工具绘制Control样式
2022-10-26 09:35:25
![](https://img.aspxhome.com/file/2023/5/69235_0s.gif)
Intellij IDEA导入JAVA项目并启动(图文教程)
2021-11-05 12:30:18
![](https://img.aspxhome.com/file/2023/7/81927_0s.jpg)
SpringBoot整合Redis之编写RedisConfig
2023-08-29 02:35:57
Java创建与结束线程代码示例
2023-01-16 16:20:00
feign 如何获取请求真实目的ip地址
2021-08-13 15:47:49
![](https://img.aspxhome.com/file/2023/1/76281_0s.jpg)
Android 动画之TranslateAnimation应用详解
2023-06-27 06:17:59
![](https://img.aspxhome.com/file/2023/1/82411_0s.png)
Flutter状态管理Bloc使用示例详解
2023-08-24 09:09:10
![](https://img.aspxhome.com/file/2023/0/98690_0s.jpg)
如何使用ByteArrayOutputStream下载文件
2021-07-29 11:32:45
Java实现邮件找回密码功能
2022-08-18 07:12:57
JavaWeb使用POI操作Excel文件实例
2022-06-12 18:19:30
![](https://img.aspxhome.com/file/2023/5/72385_0s.png)