java 金额转中文大写
时间: 2023-11-08 17:04:53 浏览: 128
以下是Java代码实现金额转中文大写:
```
import java.math.BigDecimal;
public class MoneyUtil {
// 大写数字
private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
// 整数部分单位
private static final String[] CN_UPPER_INTEGER_UNIT = {"", "拾", "佰", "仟"};
// 小数部分单位
private static final String[] CN_UPPER_FRACTION_UNIT = {"角", "分"};
/**
* 将金额转换为中文大写
*
* @param money 金额,单位为元
* @return 中文大写金额
*/
public static String moneyToChinese(BigDecimal money) {
if (money == null) {
return "";
}
StringBuilder sb = new StringBuilder();
// 处理整数部分
long integerPart = money.longValue();
if (integerPart == 0) {
sb.append(CN_UPPER_NUMBER[0]);
} else {
int i = 0;
while (integerPart > 0) {
int digit = (int) (integerPart % 10);
if (digit > 0) {
sb.insert(0, CN_UPPER_INTEGER_UNIT[i]);
sb.insert(0, CN_UPPER_NUMBER[digit]);
} else if (sb.length() > 0 && !sb.substring(0, 1).equals(CN_UPPER_NUMBER[0])) {
sb.insert(0, CN_UPPER_NUMBER[digit]);
}
integerPart /= 10;
i++;
}
}
sb.append("元");
// 处理小数部分
int fractionPart = money.movePointRight(2).intValue() % 100;
for (int i = 0; i < CN_UPPER_FRACTION_UNIT.length; i++) {
int digit = fractionPart % 10;
if (digit > 0) {
sb.append(CN_UPPER_NUMBER[digit]);
sb.append(CN_UPPER_FRACTION_UNIT[i]);
} else if (i == 0 && sb.length() > 0 && !sb.substring(sb.length() - 1).equals("元")) {
sb.append(CN_UPPER_NUMBER[digit]);
}
fractionPart /= 10;
}
if (sb.length() == 2) {
sb.append("整");
}
return sb.toString();
}
}
```
使用示例:
```
BigDecimal money = new BigDecimal("1234567.89");
String chinese = MoneyUtil.moneyToChinese(money);
System.out.println(chinese); // 壹佰贰拾叁万肆仟伍佰陆拾柒元捌角玖分
```
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)