mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add formatSimple
This commit is contained in:
parent
c5530facdf
commit
ef3e3f55b5
@ -11,6 +11,7 @@
|
|||||||
* 【core 】 StrUtil增加endWithAnyIgnoreCase(issue#I37I0B@Gitee)
|
* 【core 】 StrUtil增加endWithAnyIgnoreCase(issue#I37I0B@Gitee)
|
||||||
* 【crypto 】 Sm2增加getD和getQ方法(issue#I37Z4C@Gitee)
|
* 【crypto 】 Sm2增加getD和getQ方法(issue#I37Z4C@Gitee)
|
||||||
* 【cache 】 AbstractCache增加keySet方法(issue#I37Z4C@Gitee)
|
* 【cache 】 AbstractCache增加keySet方法(issue#I37Z4C@Gitee)
|
||||||
|
* 【core 】 NumberWordFormatter增加formatSimple方法(pr#1436@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【json 】 JSONUtil.isJson方法改变trim策略,解决特殊空白符导致判断失败问题
|
* 【json 】 JSONUtil.isJson方法改变trim策略,解决特殊空白符导致判断失败问题
|
||||||
|
@ -963,6 +963,21 @@ public class Convert {
|
|||||||
return NumberWordFormatter.format(number);
|
return NumberWordFormatter.format(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将阿拉伯数字转为精简表示形式,例如:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 1200 -> 1.2k
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param number {@link Number}对象
|
||||||
|
* @return 英文表达式
|
||||||
|
* @since 5.5.9
|
||||||
|
*/
|
||||||
|
public static String numberToSimple(Number number) {
|
||||||
|
return NumberWordFormatter.formatSimple(number.longValue());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将阿拉伯数字转为中文表达方式
|
* 将阿拉伯数字转为中文表达方式
|
||||||
*
|
*
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package cn.hutool.core.convert;
|
package cn.hutool.core.convert;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.NumberUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将浮点数类型的number转换成英语的表达方式 <br>
|
* 将浮点数类型的number转换成英语的表达方式 <br>
|
||||||
* 参考博客:http://blog.csdn.net/eric_sunah/article/details/8713226
|
* 参考博客:http://blog.csdn.net/eric_sunah/article/details/8713226
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly,totalo
|
||||||
* @since 3.0.9
|
* @since 3.0.9
|
||||||
*/
|
*/
|
||||||
public class NumberWordFormatter {
|
public class NumberWordFormatter {
|
||||||
@ -33,33 +32,37 @@ public class NumberWordFormatter {
|
|||||||
if (x != null) {
|
if (x != null) {
|
||||||
return format(x.toString());
|
return format(x.toString());
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return StrUtil.EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将阿拉伯数字转化为简介计数单位,例如 2100 => 2.1k
|
* 将阿拉伯数字转化为简洁计数单位,例如 2100 => 2.1k
|
||||||
* 范围默认只到w
|
* 范围默认只到w
|
||||||
* @param value
|
*
|
||||||
* @return
|
* @param value 被格式化的数字
|
||||||
|
* @return 格式化后的数字
|
||||||
|
* @since 5.5.9
|
||||||
*/
|
*/
|
||||||
public static String formatValue(long value) {
|
public static String formatSimple(long value) {
|
||||||
return formatValue(value, true);
|
return formatSimple(value, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将阿拉伯数字转化为简介计数单位,例如 2100 => 2.1k
|
* 将阿拉伯数字转化为简介计数单位,例如 2100 => 2.1k
|
||||||
|
*
|
||||||
* @param value 对应数字的值
|
* @param value 对应数字的值
|
||||||
* @param isTwo 控制是否为k、w
|
* @param isTwo 控制是否为只为k、w,例如当为{@code false}时返回4.38m,{@code true}返回438.43w
|
||||||
* @return
|
* @return 格式化后的数字
|
||||||
|
* @since 5.5.9
|
||||||
*/
|
*/
|
||||||
public static String formatValue(long value, boolean isTwo) {
|
public static String formatSimple(long value, boolean isTwo) {
|
||||||
if (value < 1000) {
|
if (value < 1000) {
|
||||||
return String.valueOf(value);
|
return String.valueOf(value);
|
||||||
}
|
}
|
||||||
int index = -1;
|
int index = -1;
|
||||||
double res = value * 1.0d;
|
double res = value;
|
||||||
while (res > 10 && (!isTwo || index < 1)) {
|
while (res > 10 && (false == isTwo || index < 1)) {
|
||||||
if (res > 1000) {
|
if (res > 1000) {
|
||||||
res = res / 1000;
|
res = res / 1000;
|
||||||
index++;
|
index++;
|
||||||
@ -69,8 +72,7 @@ public class NumberWordFormatter {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DecimalFormat decimalFormat = new DecimalFormat("#.##");
|
return String.format("%s%s", NumberUtil.decimalFormat("#.##", res), NUMBER_SUFFIX[index]);
|
||||||
return String.format("%s%s", decimalFormat.format(res), NUMBER_SUFFIX[index]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,14 +12,23 @@ public class NumberWordFormatTest {
|
|||||||
|
|
||||||
String format2 = NumberWordFormatter.format("2100.00");
|
String format2 = NumberWordFormatter.format("2100.00");
|
||||||
Assert.assertEquals("TWO THOUSAND ONE HUNDRED AND CENTS ONLY", format2);
|
Assert.assertEquals("TWO THOUSAND ONE HUNDRED AND CENTS ONLY", format2);
|
||||||
|
}
|
||||||
|
|
||||||
String format3 = NumberWordFormatter.formatValue(4384324, false);
|
@Test
|
||||||
Assert.assertEquals("4.38m", format3);
|
public void formatSimpleTest() {
|
||||||
|
String format1 = NumberWordFormatter.formatSimple(1200, false);
|
||||||
|
Assert.assertEquals("1.2k", format1);
|
||||||
|
|
||||||
String format4 = NumberWordFormatter.formatValue(4384324);
|
String format2 = NumberWordFormatter.formatSimple(4384324, false);
|
||||||
|
Assert.assertEquals("4.38m", format2);
|
||||||
|
|
||||||
|
String format3 = NumberWordFormatter.formatSimple(4384324, true);
|
||||||
|
Assert.assertEquals("438.43w", format3);
|
||||||
|
|
||||||
|
String format4 = NumberWordFormatter.formatSimple(4384324);
|
||||||
Assert.assertEquals("438.43w", format4);
|
Assert.assertEquals("438.43w", format4);
|
||||||
|
|
||||||
String format5 = NumberWordFormatter.formatValue(438);
|
String format5 = NumberWordFormatter.formatSimple(438);
|
||||||
Assert.assertEquals("438", format5);
|
Assert.assertEquals("438", format5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user