From 806f3c8bbe0da3dc181741bcba551f26037cd698 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 26 Aug 2025 17:32:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`NumberWordFormatter`formatSi?= =?UTF-8?q?mple=E8=BE=93=E5=87=BA=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=88pr#4034@Github=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +- .../core/convert/NumberWordFormatter.java | 4 +- .../core/convert/NumberWordFormatTest.java | 52 ++++++++++++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 155fd93aa..e06e03159 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.40(2025-08-25) +# 5.8.40(2025-08-26) ### 🐣新特性 * 【captcha】 `MathGenerator`四则运算方式支持不生成负数结果(pr#1363@Gitee) @@ -22,7 +22,8 @@ * 【extra 】 修复`QLExpressEngine`allowClassSet无效问题(issue#3994@Github) * 【core 】 修复`StrBuilder`insert插入计算错误问题(issue#ICTSRZ@Gitee) * 【cron 】 修复`CronPatternUtil.nextDateAfter`计算下一个匹配表达式的日期时,计算错误问题(issue#4006@Github) -* 【cache 】 `ReentrantCache`修改get逻辑key锁改为全局锁,保证安全。(issue#4022@Github) +* 【cache 】 `ReentrantCache`修改get逻辑key锁改为全局锁,保证安全(issue#4022@Github) +* 【core 】 修复`NumberWordFormatter`formatSimple输出错误问题(pr#4034@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.39(2025-06-20) diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/NumberWordFormatter.java b/hutool-core/src/main/java/cn/hutool/core/convert/NumberWordFormatter.java index b2536ea57..5504e8d7e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/NumberWordFormatter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/NumberWordFormatter.java @@ -66,11 +66,11 @@ public class NumberWordFormatter { } double res = value; - int index = 0; + final int index; if (isTwo) { // 当isTwo为true时,只使用k和w单位 - if (value >= 100000) { + if (value >= 10000) { // 使用w单位(除以10000,即10k = 1w) res = value / 10000.0; index = 1; // w在NUMBER_SUFFIX[1] diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/NumberWordFormatTest.java b/hutool-core/src/test/java/cn/hutool/core/convert/NumberWordFormatTest.java index 4c563b56a..b0731ca1e 100644 --- a/hutool-core/src/test/java/cn/hutool/core/convert/NumberWordFormatTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/convert/NumberWordFormatTest.java @@ -1,8 +1,10 @@ package cn.hutool.core.convert; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class NumberWordFormatTest { @Test @@ -43,4 +45,52 @@ public class NumberWordFormatTest { final String s = NumberWordFormatter.formatSimple(1000); assertEquals("1k", s); } + + @Test + public void issue4033Test(){ + String s = NumberWordFormatter.formatSimple(1_000, false); + Assertions.assertEquals("1k", s); + + s = NumberWordFormatter.formatSimple(10_000, false); + Assertions.assertEquals("10k", s); + + s = NumberWordFormatter.formatSimple(100_000, false); + Assertions.assertEquals("100k", s); + + s = NumberWordFormatter.formatSimple(1_000_000, false); + Assertions.assertEquals("1m", s); + + s = NumberWordFormatter.formatSimple(10_000_000, false); + Assertions.assertEquals("10m", s); + + s = NumberWordFormatter.formatSimple(100_000_000, false); + Assertions.assertEquals("100m", s); + + s = NumberWordFormatter.formatSimple(1_000_000_000, false); + Assertions.assertEquals("1b", s); + } + + @Test + public void issue4033Test2(){ + String s = NumberWordFormatter.formatSimple(1_000, true); + Assertions.assertEquals("1k", s); + + s = NumberWordFormatter.formatSimple(10_000, true); + Assertions.assertEquals("1w", s); + + s = NumberWordFormatter.formatSimple(100_000, true); + Assertions.assertEquals("10w", s); + + s = NumberWordFormatter.formatSimple(1_000_000, true); + Assertions.assertEquals("100w", s); + + s = NumberWordFormatter.formatSimple(10_000_000, true); + Assertions.assertEquals("1000w", s); + + s = NumberWordFormatter.formatSimple(100_000_000, true); + Assertions.assertEquals("10000w", s); + + s = NumberWordFormatter.formatSimple(1_000_000_000, true); + Assertions.assertEquals("100000w", s); + } }