一花凋零,荒芜不了整个春天。——巴尔扎克
今天在hutool
提交了个PR
https://gitee.com/dromara/hutool/pulls/536
NumberChineseFormatter.formatSimple
,用于将阿拉伯数字(支持正负整数)四舍五入后转换成中文节权位简洁计数单位,例如 -5_5555 =》 -5.56万
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
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; }
|
测试用例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @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); }
|