修复NumberWordFormatterformatSimple输出错误问题(pr#4034@Github)

This commit is contained in:
Looly
2025-08-26 17:32:36 +08:00
parent 98803bbffe
commit 806f3c8bbe
3 changed files with 56 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.40(2025-08-25) # 5.8.40(2025-08-26)
### 🐣新特性 ### 🐣新特性
* 【captcha】 `MathGenerator`四则运算方式支持不生成负数结果pr#1363@Gitee * 【captcha】 `MathGenerator`四则运算方式支持不生成负数结果pr#1363@Gitee
@@ -22,7 +22,8 @@
* 【extra 】 修复`QLExpressEngine`allowClassSet无效问题issue#3994@Github * 【extra 】 修复`QLExpressEngine`allowClassSet无效问题issue#3994@Github
* 【core 】 修复`StrBuilder`insert插入计算错误问题issue#ICTSRZ@Gitee * 【core 】 修复`StrBuilder`insert插入计算错误问题issue#ICTSRZ@Gitee
* 【cron 】 修复`CronPatternUtil.nextDateAfter`计算下一个匹配表达式的日期时计算错误问题issue#4006@Github * 【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) # 5.8.39(2025-06-20)

View File

@@ -66,11 +66,11 @@ public class NumberWordFormatter {
} }
double res = value; double res = value;
int index = 0; final int index;
if (isTwo) { if (isTwo) {
// 当isTwo为true时只使用k和w单位 // 当isTwo为true时只使用k和w单位
if (value >= 100000) { if (value >= 10000) {
// 使用w单位除以10000即10k = 1w // 使用w单位除以10000即10k = 1w
res = value / 10000.0; res = value / 10000.0;
index = 1; // w在NUMBER_SUFFIX[1] index = 1; // w在NUMBER_SUFFIX[1]

View File

@@ -1,8 +1,10 @@
package cn.hutool.core.convert; 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 org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NumberWordFormatTest { public class NumberWordFormatTest {
@Test @Test
@@ -43,4 +45,52 @@ public class NumberWordFormatTest {
final String s = NumberWordFormatter.formatSimple(1000); final String s = NumberWordFormatter.formatSimple(1000);
assertEquals("1k", s); 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);
}
} }