mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
提交NumberChineseFormatter.formatSimple,用于将阿拉伯数字(支持正负整数)四舍五入后转换成中文节权位简洁计数单位,例如 -5_5555 =》 -5.56万
This commit is contained in:
parent
7116177225
commit
b5dfc8b639
@ -2,6 +2,7 @@ package cn.hutool.core.convert;
|
|||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.NumberUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,6 +154,27 @@ public class NumberChineseFormatter {
|
|||||||
return chineseStr.toString();
|
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>
|
* 格式化-999~999之间的数字<br>
|
||||||
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"。
|
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"。
|
||||||
|
@ -175,6 +175,26 @@ public class NumberChineseFormatterTest {
|
|||||||
Assert.assertEquals("零点零伍", f1);
|
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
|
@Test
|
||||||
public void digitToChineseTest() {
|
public void digitToChineseTest() {
|
||||||
String digitToChinese = Convert.digitToChinese(12_4124_1241_2421.12);
|
String digitToChinese = Convert.digitToChinese(12_4124_1241_2421.12);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user