java金额数字转中文工具类详解
作者:阿杜_ardo 时间:2022-02-15 09:42:25
本文实例为大家分享了java金额数字转中文工具类的具体代码,供大家参考,具体内容如下
java金额数字转中文工具类ConvertNum.java
package light.mvc.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 金额数字转中文工具类
*
* @author ardo
*
*/
public class ConvertNum {
/**
* 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
* 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
*/
public static String[] ChineseNum = new String[] { "零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖" };
public static String NumToChinese(double num) {
if (num > 99999999999999.99 || num < -99999999999999.99)
throw new IllegalArgumentException(
"参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;// 正负标号
if (num < 0) {
negative = true;
num = num * (-1);
}
long temp = Math.round(num * 100);
int numFen = (int) (temp % 10);// 分
temp = temp / 10;
int numJiao = (int) (temp % 10);// 角
temp = temp / 10;
// 此时temp只包含整数部分
int[] parts = new int[20];// 将金额整数部分分为在0-9999之间数的各个部分
int numParts = 0;// 记录把原来金额整数部分分割为几个部分
for (int i = 0;; i++) {
if (temp == 0)
break;
int part = (int) (temp % 10000);
parts[i] = part;
temp = temp / 10000;
numParts++;
}
boolean beforeWanIsZero = true;// 标志位,记录万的下一级是否为0
String chineseStr = "";
for (int i = 0; i < numParts; i++) {
String partChinese = partConvert(parts[i]);
if (i % 2 == 0) {
if ("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if (i != 0) {
if (i % 2 == 0)// 亿的部分
chineseStr = "亿" + chineseStr;
else {
if ("".equals(partChinese) && !beforeWanIsZero)// 如果“万”对应的
// part 为
// 0,而“万”下面一级不为
// 0,则不加“万”,而加“零”
chineseStr = "零" + chineseStr;
else {
if (parts[i - 1] < 1000 && parts[i - 1] > 0)// 如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
chineseStr = "零" + chineseStr;
chineseStr = "万" + chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if ("".equals(chineseStr))// 整数部分为0,则表示为零元
chineseStr = ChineseNum[0];
else if (negative)// 整数部分部位0,但是为负数
chineseStr = "负" + chineseStr;
chineseStr = chineseStr + "元";
if (numFen == 0 && numJiao == 0) {
chineseStr = chineseStr + "整";
} else if (numFen == 0) {// 0分
chineseStr = chineseStr + ChineseNum[numJiao] + "角";
} else {
if (numJiao == 0)
chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
else
chineseStr = chineseStr + ChineseNum[numJiao] + "角"
+ ChineseNum[numFen] + "分";
}
return chineseStr;
}
// 转换拆分后的每个部分,0-9999之间
public static String partConvert(int partNum) {
if (partNum < 0 || partNum > 10000) {
throw new IllegalArgumentException("参数必须是大于等于0或小于10000的整数");
}
String[] units = new String[] { "", "拾", "佰", "仟" };
int temp = partNum;
String partResult = new Integer(partNum).toString();
int partResultLength = partResult.length();
boolean lastIsZero = true;// 记录上一位是否为0
String chineseStr = "";
for (int i = 0; i < partResultLength; i++) {
if (temp == 0)// 高位无数字
break;
int digit = temp % 10;
if (digit == 0) {
if (!lastIsZero)// 如果前一个数字不是0则在当前汉字串前加零
chineseStr = "零" + chineseStr;
lastIsZero = true;
} else {
chineseStr = ChineseNum[digit] + units[i] + chineseStr;
lastIsZero = false;
}
temp = temp / 10;
}
return chineseStr;
}
public static void main(String args[]) {
double num = 0;
System.out.println("请输入金额数字,-1退出");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
num = Double.parseDouble(br.readLine());
} catch (IOException e) {
System.out.println(e.toString());
}
while (num != -1) {
System.out.println(num + NumToChinese(num));
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
num = Double.parseDouble(br.readLine());
} catch (IOException e) {
System.out.println(e.toString());
}
}
System.out.println("其他测试:");
System.out.println("100120: " + NumToChinese(100120));
System.out.println("25000000000005.999: "
+ NumToChinese(25000000000005.999));
System.out.println("45689263.626: " + NumToChinese(45689263.626));
System.out.println("0.69457: " + NumToChinese(0.69457));
System.out.println("253.0: " + NumToChinese(253.0));
System.out.println("0: " + NumToChinese(0));
}
}
来源:https://blog.csdn.net/ardo_pass/article/details/78703030
标签:java,金额数字,中文
0
投稿
猜你喜欢
SpringMVC使用RESTful接口案例
2021-10-10 20:03:45
Kotlin四大组件中的broadcast广播
2023-07-08 00:44:08
Android中通过RxJava进行响应式程序设计的入门指南
2023-06-27 08:17:46
springboot自定义starter方法及注解实例
2022-11-02 10:52:08
Java Lock锁多线程中实现流水线任务
2023-08-09 17:44:02
分享C#操作内存读写方法的主要实现代码
2022-10-08 08:08:44
Java中BufferedReader与BufferedWriter类的使用示例
2022-10-30 01:00:04
详解APP微信支付(java后台_统一下单和回调)
2023-11-10 17:26:42
关于springboot2整合lettuce启动卡住问题的解决方法
2022-08-24 09:29:16
Java IO学习之缓冲输入流(BufferedInputStream)
2021-10-08 22:30:23
opencv实现读取视频保存视频
2021-08-16 15:57:50
Android多边形区域扫描线种子填充算法的示例
2023-05-25 00:27:24
C#动态执行批处理命令的方法
2023-03-16 23:19:40
Android中通过view方式获取当前Activity的屏幕截图实现方法
2021-09-20 09:17:22
Java全面分析面向对象之继承
2023-11-23 11:55:59
Redis缓存策略超详细讲解
2023-11-08 06:20:10
浅谈C#中对引用类型的误解
2021-10-18 12:40:37
ResultSet如何动态获取列名和值
2022-01-16 15:54:01
Android弹窗ListPopupWindow的简单应用详解
2021-08-04 19:42:52
Java实现简易学生管理系统
2022-10-16 19:26:49