From b5dfc8b6397daeb193633a8305df602a32edd157 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Thu, 10 Feb 2022 21:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4NumberChineseFormatter.format?= =?UTF-8?q?Simple=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=B0=86=E9=98=BF=E6=8B=89?= =?UTF-8?q?=E4=BC=AF=E6=95=B0=E5=AD=97=EF=BC=88=E6=94=AF=E6=8C=81=E6=AD=A3?= =?UTF-8?q?=E8=B4=9F=E6=95=B4=E6=95=B0=EF=BC=89=E5=9B=9B=E8=88=8D=E4=BA=94?= =?UTF-8?q?=E5=85=A5=E5=90=8E=E8=BD=AC=E6=8D=A2=E6=88=90=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E8=8A=82=E6=9D=83=E4=BD=8D=E7=AE=80=E6=B4=81=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E5=8D=95=E4=BD=8D=EF=BC=8C=E4=BE=8B=E5=A6=82=20-5=5F5555=20=3D?= =?UTF-8?q?=E3=80=8B=20-5.56=E4=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/convert/NumberChineseFormatter.java | 22 +++++++++++++++++++ .../convert/NumberChineseFormatterTest.java | 20 +++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java b/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java index 29c2405e1..d6da1d22b 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java @@ -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之间的数字
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"。 diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java b/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java index f9bec8251..5dc83a714 100644 --- a/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java @@ -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);