Java将数字金额转换成中文大写形式
在项目中有时候需要将数字金额转成中文形式,如120.23为“壹佰贰拾圆贰角叁分“,下面是代码实现文章来源地址https://www.toymoban.com/news/detail-734719.html
import java.math.BigDecimal;
import java.math.RoundingMode;
public class ChineseMoneyUtils {
/**
* 中文数字
*/
final static private String[] CHINESE_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
/**
* 中文数字单位
*/
final static private String[] CHINESE_NUMBER_UNIT = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };
/**
* 人民币单位
*/
final static private String[] CHINESE_MONEY_UNIT = { "圆", "角", "分" };
public static void main(String[] args) {
String chineseMoney = toChineseMoney(new BigDecimal("320"));
System.out.println("chineseMoney = " + chineseMoney);
}
/**
* @param sourceMoney 要转换的数值,最多支持到亿
* @return 结果
*/
public static String toChineseMoney(BigDecimal sourceMoney) {
if (new BigDecimal("1000000000000").compareTo(sourceMoney) <= 0
&& BigDecimal.ZERO.compareTo(sourceMoney) >= 0) {
throw new RuntimeException("支持转换的金额范围为0~1万亿");
}
StringBuilder sb = new StringBuilder();
// 整数部分
BigDecimal intPart = sourceMoney.setScale(0, RoundingMode.DOWN);
// 小数部分
BigDecimal decimalPart = sourceMoney.subtract(intPart).multiply(new BigDecimal(100)).setScale(0,
RoundingMode.DOWN);
// 处理整数部分圆
if (intPart.compareTo(BigDecimal.ZERO) > 0) {
String intPartNumberString = intPart.toPlainString();
int length = intPartNumberString.length();
// 统计末尾的零,末尾零不做处理
int zeroCount = 0;
for (int i = length - 1; i >= 0; i--) {
int number = Integer.parseInt(String.valueOf(intPartNumberString.charAt(i)));
if (number == 0) {
zeroCount++;
} else {
break;
}
}
for (int i = 0; i < length; i++) {
// 如果转换到末尾0,则停止转换
if (i + zeroCount == length) {
break;
}
int number = Integer.parseInt(String.valueOf(intPartNumberString.charAt(i)));
// 获取中文数字
String chineseNumber = CHINESE_NUMBER[number];
// 获取中文数字单位
String chineseNumberUnit = CHINESE_NUMBER_UNIT[length - i - 1];
sb.append(chineseNumber).append(chineseNumberUnit);
}
// 统计完后加上金额单位
sb.append(CHINESE_MONEY_UNIT[0]);
} else {
sb.append(CHINESE_NUMBER[0]).append(CHINESE_MONEY_UNIT[0]);
}
// 处理小数部分
if (decimalPart.compareTo(new BigDecimal(10)) >= 0) {
// 角
String jiao = decimalPart.toPlainString();
int number = Integer.parseInt(String.valueOf(jiao.charAt(0)));
if (number != 0) {
String chineseNumber = CHINESE_NUMBER[number];
sb.append(chineseNumber).append(CHINESE_MONEY_UNIT[1]);
}
// 分
String fen = decimalPart.toPlainString();
number = Integer.parseInt(String.valueOf(fen.charAt(1)));
if (number != 0) {
String chineseNumber = CHINESE_NUMBER[number];
sb.append(chineseNumber).append(CHINESE_MONEY_UNIT[2]);
}
} else if (decimalPart.compareTo(BigDecimal.ZERO) > 0) {
// 分
String fen = decimalPart.toPlainString();
int number = Integer.parseInt(String.valueOf(fen.charAt(0)));
if (number != 0) {
String chineseNumber = CHINESE_NUMBER[number];
sb.append(chineseNumber).append(CHINESE_MONEY_UNIT[2]);
}
} else {
sb.append("整");
}
return sb.toString();
}
}
文章来源:https://www.toymoban.com/news/detail-734719.html
到了这里,关于Java将数字金额转换成中文大写形式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!