mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
阿拉伯数字转换成中文对发票票面金额转换的扩展
This commit is contained in:
parent
b8bfce490b
commit
206de3bad0
@ -40,7 +40,6 @@ public class NumberChineseFormatter {
|
||||
new ChineseUnit('亿', 1_0000_0000, true),
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数、小数的转换.
|
||||
*
|
||||
@ -53,15 +52,25 @@ public class NumberChineseFormatter {
|
||||
}
|
||||
|
||||
/**
|
||||
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数、小数的转换.
|
||||
* 阿拉伯数字转换成中文.
|
||||
*
|
||||
* <p>主要是对发票票面金额转换的扩展
|
||||
* <p>如:-12.32
|
||||
* <p>发票票面转换为:(负数)壹拾贰圆叁角贰分
|
||||
* <p>而非:负壹拾贰元叁角贰分
|
||||
* <p>共两点不同:1、(负数) 而非 负;2、圆 而非 元
|
||||
* 2022/3/9
|
||||
*
|
||||
* @param amount 数字
|
||||
* @param isUseTraditional 是否使用繁体
|
||||
* @param isMoneyMode 是否为金额模式
|
||||
* @return 中文
|
||||
* @param isMoneyMode 是否金额模式
|
||||
* @param negativeName 负号转换名称 如:负、(负数)
|
||||
* @param unitName 单位名称 如:元、圆
|
||||
* @return java.lang.String
|
||||
* @author machuanpeng
|
||||
*/
|
||||
public static String format(double amount, boolean isUseTraditional, boolean isMoneyMode) {
|
||||
if(0 == amount){
|
||||
public static String format(double amount, boolean isUseTraditional, boolean isMoneyMode, String negativeName, String unitName) {
|
||||
if (0 == amount) {
|
||||
return "零";
|
||||
}
|
||||
Assert.checkBetween(amount, -99_9999_9999_9999.99, 99_9999_9999_9999.99,
|
||||
@ -71,7 +80,7 @@ public class NumberChineseFormatter {
|
||||
|
||||
// 负数
|
||||
if (amount < 0) {
|
||||
chineseStr.append("负");
|
||||
chineseStr.append(StrUtil.isNullOrUndefined(negativeName) ? "负" : negativeName);
|
||||
amount = -amount;
|
||||
}
|
||||
|
||||
@ -82,44 +91,44 @@ public class NumberChineseFormatter {
|
||||
yuan = yuan / 10;
|
||||
|
||||
// 元
|
||||
if(false == isMoneyMode || 0 != yuan){
|
||||
if (false == isMoneyMode || 0 != yuan) {
|
||||
// 金额模式下,无需“零元”
|
||||
chineseStr.append(longToChinese(yuan, isUseTraditional));
|
||||
if(isMoneyMode){
|
||||
chineseStr.append("元");
|
||||
if (isMoneyMode) {
|
||||
chineseStr.append(StrUtil.isNullOrUndefined(unitName) ? "元" : unitName);
|
||||
}
|
||||
}
|
||||
|
||||
if(0 == jiao && 0 == fen){
|
||||
if (0 == jiao && 0 == fen) {
|
||||
//无小数部分的金额结尾
|
||||
if(isMoneyMode){
|
||||
if (isMoneyMode) {
|
||||
chineseStr.append("整");
|
||||
}
|
||||
return chineseStr.toString();
|
||||
}
|
||||
|
||||
// 小数部分
|
||||
if(false == isMoneyMode){
|
||||
if (false == isMoneyMode) {
|
||||
chineseStr.append("点");
|
||||
}
|
||||
|
||||
// 角
|
||||
if(0 == yuan && 0 == jiao){
|
||||
if (0 == yuan && 0 == jiao) {
|
||||
// 元和角都为0时,只有非金额模式下补“零”
|
||||
if(false == isMoneyMode){
|
||||
if (false == isMoneyMode) {
|
||||
chineseStr.append("零");
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
chineseStr.append(numberToChinese(jiao, isUseTraditional));
|
||||
if(isMoneyMode && 0 != jiao){
|
||||
if (isMoneyMode && 0 != jiao) {
|
||||
chineseStr.append("角");
|
||||
}
|
||||
}
|
||||
|
||||
// 分
|
||||
if(0 != fen){
|
||||
if (0 != fen) {
|
||||
chineseStr.append(numberToChinese(fen, isUseTraditional));
|
||||
if(isMoneyMode){
|
||||
if (isMoneyMode) {
|
||||
chineseStr.append("分");
|
||||
}
|
||||
}
|
||||
@ -127,16 +136,28 @@ public class NumberChineseFormatter {
|
||||
return chineseStr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数、小数的转换.
|
||||
*
|
||||
* @param amount 数字
|
||||
* @param isUseTraditional 是否使用繁体
|
||||
* @param isMoneyMode 是否为金额模式
|
||||
* @return 中文
|
||||
*/
|
||||
public static String format(double amount, boolean isUseTraditional, boolean isMoneyMode) {
|
||||
return format(amount, isUseTraditional, isMoneyMode, "负", "元");
|
||||
}
|
||||
|
||||
/**
|
||||
* 阿拉伯数字(支持正负整数)转换成中文
|
||||
*
|
||||
* @param amount 数字
|
||||
* @param amount 数字
|
||||
* @param isUseTraditional 是否使用繁体
|
||||
* @return 中文
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public static String format(long amount, boolean isUseTraditional){
|
||||
if(0 == amount){
|
||||
public static String format(long amount, boolean isUseTraditional) {
|
||||
if (0 == amount) {
|
||||
return "零";
|
||||
}
|
||||
Assert.checkBetween(amount, -99_9999_9999_9999.99, 99_9999_9999_9999.99,
|
||||
@ -179,16 +200,16 @@ public class NumberChineseFormatter {
|
||||
* 格式化-999~999之间的数字<br>
|
||||
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"。
|
||||
*
|
||||
* @param amount 数字
|
||||
* @param amount 数字
|
||||
* @param isUseTraditional 是否使用繁体
|
||||
* @return 中文
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public static String formatThousand(int amount, boolean isUseTraditional){
|
||||
public static String formatThousand(int amount, boolean isUseTraditional) {
|
||||
Assert.checkBetween(amount, -999, 999, "Number support only: (-999 ~ 999)!");
|
||||
|
||||
final String chinese = thousandToChinese(amount, isUseTraditional);
|
||||
if(amount < 20 && amount >= 10){
|
||||
if (amount < 20 && amount >= 10) {
|
||||
// "十一"而非"一十一"
|
||||
return chinese.substring(1);
|
||||
}
|
||||
@ -218,7 +239,7 @@ public class NumberChineseFormatter {
|
||||
* @return 中文
|
||||
*/
|
||||
private static String longToChinese(long amount, boolean isUseTraditional) {
|
||||
if(0 == amount){
|
||||
if (0 == amount) {
|
||||
return "零";
|
||||
}
|
||||
|
||||
@ -235,11 +256,11 @@ public class NumberChineseFormatter {
|
||||
|
||||
// 千
|
||||
partValue = parts[0];
|
||||
if(partValue > 0){
|
||||
if (partValue > 0) {
|
||||
partChinese = thousandToChinese(partValue, isUseTraditional);
|
||||
chineseStr.insert(0, partChinese);
|
||||
|
||||
if(partValue < 1000){
|
||||
if (partValue < 1000) {
|
||||
// 和万位之间空0,则补零,如一万零三百
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
@ -247,26 +268,26 @@ public class NumberChineseFormatter {
|
||||
|
||||
// 万
|
||||
partValue = parts[1];
|
||||
if(partValue > 0){
|
||||
if((partValue % 10 == 0 && parts[0] > 0)){
|
||||
if (partValue > 0) {
|
||||
if ((partValue % 10 == 0 && parts[0] > 0)) {
|
||||
// 如果"万"的个位是0,则补零,如十万零八千
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
partChinese = thousandToChinese(partValue, isUseTraditional);
|
||||
chineseStr.insert(0, partChinese + "万");
|
||||
|
||||
if(partValue < 1000){
|
||||
if (partValue < 1000) {
|
||||
// 和亿位之间空0,则补零,如一亿零三百万
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
} else{
|
||||
} else {
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
|
||||
// 亿
|
||||
partValue = parts[2];
|
||||
if(partValue > 0){
|
||||
if((partValue % 10 == 0 && parts[1] > 0)){
|
||||
if (partValue > 0) {
|
||||
if ((partValue % 10 == 0 && parts[1] > 0)) {
|
||||
// 如果"万"的个位是0,则补零,如十万零八千
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
@ -274,25 +295,25 @@ public class NumberChineseFormatter {
|
||||
partChinese = thousandToChinese(partValue, isUseTraditional);
|
||||
chineseStr.insert(0, partChinese + "亿");
|
||||
|
||||
if(partValue < 1000){
|
||||
if (partValue < 1000) {
|
||||
// 和万亿位之间空0,则补零,如一万亿零三百亿
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
} else{
|
||||
} else {
|
||||
addPreZero(chineseStr);
|
||||
}
|
||||
|
||||
// 万亿
|
||||
partValue = parts[3];
|
||||
if(partValue > 0){
|
||||
if(parts[2] == 0){
|
||||
if (partValue > 0) {
|
||||
if (parts[2] == 0) {
|
||||
chineseStr.insert(0, "亿");
|
||||
}
|
||||
partChinese = thousandToChinese(partValue, isUseTraditional);
|
||||
chineseStr.insert(0, partChinese + "万");
|
||||
}
|
||||
|
||||
if(StrUtil.isNotEmpty(chineseStr) && '零' == chineseStr.charAt(0)){
|
||||
if (StrUtil.isNotEmpty(chineseStr) && '零' == chineseStr.charAt(0)) {
|
||||
return chineseStr.substring(1);
|
||||
}
|
||||
|
||||
@ -386,7 +407,7 @@ public class NumberChineseFormatter {
|
||||
} else {
|
||||
// 非节单位,和单位前的单数字组合为值
|
||||
int unitNumber = number;
|
||||
if(0 == number && 0 == i){
|
||||
if (0 == number && 0 == i) {
|
||||
// issue#1726,对于单位开头的数组,默认赋予1
|
||||
// 十二 -> 一十二
|
||||
// 百二 -> 一百二
|
||||
@ -502,12 +523,12 @@ public class NumberChineseFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addPreZero(StringBuilder chineseStr){
|
||||
if(StrUtil.isEmpty(chineseStr)){
|
||||
private static void addPreZero(StringBuilder chineseStr) {
|
||||
if (StrUtil.isEmpty(chineseStr)) {
|
||||
return;
|
||||
}
|
||||
final char c = chineseStr.charAt(0);
|
||||
if('零' != c){
|
||||
if ('零' != c) {
|
||||
chineseStr.insert(0, '零');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user