!536 提交将阿拉伯数字四舍五入后转换成中文节权位简洁记录单位

Merge pull request !536 from 阿超/v5-dev
This commit is contained in:
Looly 2022-02-11 05:37:12 +00:00 committed by Gitee
commit 703ae88ddc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 42 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package cn.hutool.core.convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
/**
@ -153,6 +154,27 @@ public class NumberChineseFormatter {
return chineseStr.toString();
}
/**
* 阿拉伯数字支持正负整数四舍五入后转换成中文节权位简洁计数单位例如 -5_5555 = -5.56万
*
* @param amount 数字
* @return 中文
*/
public static String formatSimple(long amount) {
if (amount < 1_0000 && amount > -1_0000) {
return String.valueOf(amount);
}
String res;
if (amount < 1_0000_0000 && amount > -1_0000_0000) {
res = NumberUtil.div(amount, 1_0000, 2) + "";
} else if (amount < 1_0000_0000_0000L && amount > -1_0000_0000_0000L) {
res = NumberUtil.div(amount, 1_0000_0000, 2) + "亿";
} else {
res = NumberUtil.div(amount, 1_0000_0000_0000L, 2) + "万亿";
}
return res;
}
/**
* 格式化-999~999之间的数字<br>
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"

View File

@ -175,6 +175,26 @@ public class NumberChineseFormatterTest {
Assert.assertEquals("零点零伍", f1);
}
@Test
public void formatSimpleTest() {
String f1 = NumberChineseFormatter.formatSimple(1_2345);
Assert.assertEquals("1.23万", f1);
f1 = NumberChineseFormatter.formatSimple(-5_5555);
Assert.assertEquals("-5.56万", f1);
f1 = NumberChineseFormatter.formatSimple(1_2345_6789);
Assert.assertEquals("1.23亿", f1);
f1 = NumberChineseFormatter.formatSimple(-5_5555_5555);
Assert.assertEquals("-5.56亿", f1);
f1 = NumberChineseFormatter.formatSimple(1_2345_6789_1011L);
Assert.assertEquals("1.23万亿", f1);
f1 = NumberChineseFormatter.formatSimple(-5_5555_5555_5555L);
Assert.assertEquals("-5.56万亿", f1);
f1 = NumberChineseFormatter.formatSimple(123);
Assert.assertEquals("123", f1);
f1 = NumberChineseFormatter.formatSimple(-123);
Assert.assertEquals("-123", f1);
}
@Test
public void digitToChineseTest() {
String digitToChinese = Convert.digitToChinese(12_4124_1241_2421.12);