Merge pull request #4034 from sunshineflymeat/hutool-0825-1829

Fix issue 4033
This commit is contained in:
Golden Looly
2025-08-26 17:25:33 +08:00
committed by GitHub
2 changed files with 37 additions and 17 deletions

View File

@@ -13,15 +13,18 @@ import cn.hutool.core.util.StrUtil;
*/
public class NumberWordFormatter {
private static final String[] NUMBER = new String[]{"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE"};
private static final String[] NUMBER_TEEN = new String[]{"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
"FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"};
private static final String[] NUMBER_TEN = new String[]{"TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY",
"SEVENTY", "EIGHTY", "NINETY"};
private static final String[] NUMBER_MORE = new String[]{"", "THOUSAND", "MILLION", "BILLION", "TRILLION"};
private static final String[] NUMBER = new String[] { "", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE" };
private static final String[] NUMBER_TEEN = new String[] { "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
"FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" };
private static final String[] NUMBER_TEN = new String[] { "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY",
"SEVENTY", "EIGHTY", "NINETY" };
private static final String[] NUMBER_MORE = new String[] { "", "THOUSAND", "MILLION", "BILLION", "TRILLION" };
private static final String[] NUMBER_SUFFIX = new String[]{"k", "w", "", "m", "", "", "b", "", "", "t", "", "", "p", "", "", "e"};
private static final String[] NUMBER_SUFFIX = new String[] { "k", "w", "m", "b", "t", "p", "e" };
// 标准单位序列(k, m, b, t, p, e)在NUMBER_SUFFIX中的索引
private static final int[] STANDARD_UNIT_INDICES = { 0, 2, 3, 4, 5, 6 };
/**
* 将阿拉伯数字转为英文表达式
@@ -61,18 +64,32 @@ public class NumberWordFormatter {
if (value < 1000) {
return String.valueOf(value);
}
int index = -1;
double res = value;
while (res > 10 && (false == isTwo || index < 1)) {
if (res >= 1000) {
int index = 0;
if (isTwo) {
// 当isTwo为true时只使用k和w单位
if (value >= 100000) {
// 使用w单位除以10000即10k = 1w
res = value / 10000.0;
index = 1; // w在NUMBER_SUFFIX[1]
} else {
// 使用k单位除以1000
res = value / 1000.0;
index = 0; // k在NUMBER_SUFFIX[0]
}
} else {
// 当isTwo为false时使用标准单位序列 (k, m, b, t, p, e)
// 对应NUMBER_SUFFIX中的索引为 0, 2, 3, 4, 5, 6
int unitIndex = -1;
while (res >= 1000 && unitIndex < STANDARD_UNIT_INDICES.length - 1) {
res = res / 1000;
index++;
}
if (res > 10) {
res = res / 10;
index++;
unitIndex++;
}
index = STANDARD_UNIT_INDICES[unitIndex];
}
return String.format("%s%s", NumberUtil.decimalFormat("#.##", res), NUMBER_SUFFIX[index]);
}
@@ -107,7 +124,7 @@ public class NumberWordFormatter {
StringBuilder lm = new StringBuilder(); // 用来存放转换后的整数部分
for (int i = 0; i < lstrrev.length() / 3; i++) {
a[i] = StrUtil.reverse(lstrrev.substring(3 * i, 3 * i + 3)); // 截取第一个三位
if (false == "000".equals(a[i])) { // 用来避免这种情况1000000 = one million
if (!"000".equals(a[i])) { // 用来避免这种情况1000000 = one million
// thousand only
if (i != 0) {
lm.insert(0, transThree(a[i]) + " " + parseMore(i) + " "); // 加:

View File

@@ -33,6 +33,9 @@ public class NumberWordFormatTest {
final String format5 = NumberWordFormatter.formatSimple(438);
assertEquals("438", format5);
final String format6 = NumberWordFormatter.formatSimple(1000000, false);
assertEquals("1m", format6);
}
@Test