diff --git a/CHANGELOG.md b/CHANGELOG.md index b332bdba5..24a1efd63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * 【core 】 StrUtil增加endWithAnyIgnoreCase(issue#I37I0B@Gitee) * 【crypto 】 Sm2增加getD和getQ方法(issue#I37Z4C@Gitee) * 【cache 】 AbstractCache增加keySet方法(issue#I37Z4C@Gitee) +* 【core 】 NumberWordFormatter增加formatSimple方法(pr#1436@Github) ### Bug修复 * 【json 】 JSONUtil.isJson方法改变trim策略,解决特殊空白符导致判断失败问题 diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java index dc1324284..2e2377f32 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java @@ -28,9 +28,9 @@ import java.util.concurrent.TimeUnit; /** * 类型转换器 - * + * * @author xiaoleilu - * + * */ public class Convert { @@ -38,7 +38,7 @@ public class Convert { * 转换为字符串
* 如果给定的值为null,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -51,17 +51,17 @@ public class Convert { * 转换为字符串
* 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ public static String toStr(Object value) { return toStr(value, null); } - + /** * 转换为String数组 - * + * * @param value 被转换的值 * @return String数组 * @since 3.2.0 @@ -74,7 +74,7 @@ public class Convert { * 转换为字符
* 如果给定的值为null,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -87,17 +87,17 @@ public class Convert { * 转换为字符
* 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ public static Character toChar(Object value) { return toChar(value, null); } - + /** * 转换为Character数组 - * + * * @param value 被转换的值 * @return Character数组 * @since 3.2.0 @@ -110,7 +110,7 @@ public class Convert { * 转换为byte
* 如果给定的值为{@code null},或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -123,17 +123,17 @@ public class Convert { * 转换为byte
* 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ public static Byte toByte(Object value) { return toByte(value, null); } - + /** * 转换为Byte数组 - * + * * @param value 被转换的值 * @return Byte数组 * @since 3.2.0 @@ -157,7 +157,7 @@ public class Convert { * 转换为Short
* 如果给定的值为{@code null},或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -170,17 +170,17 @@ public class Convert { * 转换为Short
* 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ public static Short toShort(Object value) { return toShort(value, null); } - + /** * 转换为Short数组 - * + * * @param value 被转换的值 * @return Short数组 * @since 3.2.0 @@ -193,7 +193,7 @@ public class Convert { * 转换为Number
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -206,17 +206,17 @@ public class Convert { * 转换为Number
* 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ public static Number toNumber(Object value) { return toNumber(value, null); } - + /** * 转换为Number数组 - * + * * @param value 被转换的值 * @return Number数组 * @since 3.2.0 @@ -229,7 +229,7 @@ public class Convert { * 转换为int
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -242,7 +242,7 @@ public class Convert { * 转换为int
* 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ @@ -264,7 +264,7 @@ public class Convert { * 转换为long
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -277,7 +277,7 @@ public class Convert { * 转换为long
* 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ @@ -287,7 +287,7 @@ public class Convert { /** * 转换为Long数组
- * + * * @param value 被转换的值 * @return 结果 */ @@ -299,7 +299,7 @@ public class Convert { * 转换为double
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -312,7 +312,7 @@ public class Convert { * 转换为double
* 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ @@ -322,7 +322,7 @@ public class Convert { /** * 转换为Double数组
- * + * * @param value 被转换的值 * @return 结果 */ @@ -334,7 +334,7 @@ public class Convert { * 转换为Float
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -347,7 +347,7 @@ public class Convert { * 转换为Float
* 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ @@ -357,7 +357,7 @@ public class Convert { /** * 转换为Float数组
- * + * * @param value 被转换的值 * @return 结果 */ @@ -369,7 +369,7 @@ public class Convert { * 转换为boolean
* String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -382,7 +382,7 @@ public class Convert { * 转换为boolean
* 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ @@ -392,7 +392,7 @@ public class Convert { /** * 转换为Boolean数组
- * + * * @param value 被转换的值 * @return 结果 */ @@ -404,7 +404,7 @@ public class Convert { * 转换为BigInteger
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -417,7 +417,7 @@ public class Convert { * 转换为BigInteger
* 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ @@ -429,7 +429,7 @@ public class Convert { * 转换为BigDecimal
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -442,19 +442,19 @@ public class Convert { * 转换为BigDecimal
* 如果给定的值为空,或者转换失败,返回null
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 */ public static BigDecimal toBigDecimal(Object value) { return toBigDecimal(value, null); } - + /** * 转换为Date
* 如果给定的值为空,或者转换失败,返回默认值
* 转换失败不会报错 - * + * * @param value 被转换的值 * @param defaultValue 转换错误时的默认值 * @return 结果 @@ -489,7 +489,7 @@ public class Convert { public static LocalDateTime toLocalDateTime(Object value) { return toLocalDateTime(value, null); } - + /** * Instant
* 如果给定的值为空,或者转换失败,返回默认值
@@ -508,7 +508,7 @@ public class Convert { * 转换为Date
* 如果给定的值为空,或者转换失败,返回{@code null}
* 转换失败不会报错 - * + * * @param value 被转换的值 * @return 结果 * @since 4.1.6 @@ -516,11 +516,11 @@ public class Convert { public static Date toDate(Object value) { return toDate(value, null); } - + /** * 转换为Enum对象
* 如果给定的值为空,或者转换失败,返回默认值
- * + * * @param 枚举类型 * @param clazz Enum的Class * @param value 值 @@ -535,7 +535,7 @@ public class Convert { /** * 转换为Enum对象
* 如果给定的值为空,或者转换失败,返回默认值{@code null}
- * + * * @param 枚举类型 * @param clazz Enum的Class * @param value 值 @@ -547,7 +547,7 @@ public class Convert { /** * 转换为集合类 - * + * * @param collectionType 集合类型 * @param elementType 集合中元素类型 * @param value 被转换的值 @@ -557,10 +557,10 @@ public class Convert { public static Collection toCollection(Class collectionType, Class elementType, Object value) { return new CollectionConverter(collectionType, elementType).convert(value, null); } - + /** * 转换为ArrayList,元素类型默认Object - * + * * @param value 被转换的值 * @return {@link List} * @since 4.1.11 @@ -568,10 +568,10 @@ public class Convert { public static List toList(Object value) { return convert(List.class, value); } - + /** * 转换为ArrayList - * + * * @param 元素类型 * @param elementType 集合中元素类型 * @param value 被转换的值 @@ -598,10 +598,10 @@ public class Convert { public static Map toMap(Class keyType, Class valueType, Object value) { return (Map) new MapConverter(HashMap.class, keyType, valueType).convert(value, null); } - + /** * 转换值为指定类型,类型采用字符串表示 - * + * * @param 目标类型 * @param className 类的字符串表示 * @param value 值 @@ -612,10 +612,10 @@ public class Convert { public static T convertByClassName(String className, Object value) throws ConvertException{ return convert(ClassUtil.loadClass(className), value); } - + /** * 转换值为指定类型 - * + * * @param 目标类型 * @param type 类型 * @param value 值 @@ -626,10 +626,10 @@ public class Convert { public static T convert(Class type, Object value) throws ConvertException{ return convert((Type)type, value); } - + /** * 转换值为指定类型 - * + * * @param 目标类型 * @param reference 类型参考,用于持有转换后的泛型类型 * @param value 值 @@ -642,7 +642,7 @@ public class Convert { /** * 转换值为指定类型 - * + * * @param 目标类型 * @param type 类型 * @param value 值 @@ -652,10 +652,10 @@ public class Convert { public static T convert(Type type, Object value) throws ConvertException{ return convert(type, value, null); } - + /** * 转换值为指定类型 - * + * * @param 目标类型 * @param type 类型 * @param value 值 @@ -667,10 +667,10 @@ public class Convert { public static T convert(Class type, Object value, T defaultValue) throws ConvertException { return convert((Type)type, value, defaultValue); } - + /** * 转换值为指定类型 - * + * * @param 目标类型 * @param type 类型 * @param value 值 @@ -681,11 +681,11 @@ public class Convert { public static T convert(Type type, Object value, T defaultValue) throws ConvertException { return convertWithCheck(type, value, defaultValue, false); } - + /** * 转换值为指定类型,不抛异常转换
* 当转换失败时返回{@code null} - * + * * @param 目标类型 * @param type 目标类型 * @param value 值 @@ -695,11 +695,11 @@ public class Convert { public static T convertQuietly(Type type, Object value) { return convertQuietly(type, value, null); } - + /** * 转换值为指定类型,不抛异常转换
* 当转换失败时返回默认值 - * + * * @param 目标类型 * @param type 目标类型 * @param value 值 @@ -734,11 +734,11 @@ public class Convert { throw e; } } - + // ----------------------------------------------------------------------- 全角半角转换 /** * 半角转全角 - * + * * @param input String. * @return 全角字符串. */ @@ -748,7 +748,7 @@ public class Convert { /** * 半角转全角 - * + * * @param input String * @param notConvertSet 不替换的字符集合 * @return 全角字符串. @@ -773,7 +773,7 @@ public class Convert { /** * 全角转半角 - * + * * @param input String. * @return 半角字符串 */ @@ -783,7 +783,7 @@ public class Convert { /** * 替换全角为半角 - * + * * @param text 文本 * @param notConvertSet 不替换的字符集合 * @return 替换后的字符 @@ -813,7 +813,7 @@ public class Convert { // --------------------------------------------------------------------- hex /** * 字符串转换成十六进制字符串,结果为小写 - * + * * @param str 待转换的ASCII字符串 * @param charset 编码 * @return 16进制字符串 @@ -825,7 +825,7 @@ public class Convert { /** * byte数组转16进制串 - * + * * @param bytes 被转换的byte数组 * @return 转换后的值 * @see HexUtil#encodeHexStr(byte[]) @@ -836,7 +836,7 @@ public class Convert { /** * Hex字符串转换为Byte值 - * + * * @param src Byte字符串,每个Byte之间没有分隔符 * @return byte[] * @see HexUtil#decodeHex(char[]) @@ -847,7 +847,7 @@ public class Convert { /** * 十六进制转换字符串 - * + * * @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B]) * @param charset 编码 {@link Charset} * @return 对应的字符串 @@ -858,10 +858,10 @@ public class Convert { public static String hexStrToStr(String hexStr, Charset charset) { return hexToStr(hexStr, charset); } - + /** * 十六进制转换字符串 - * + * * @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B]) * @param charset 编码 {@link Charset} * @return 对应的字符串 @@ -874,7 +874,7 @@ public class Convert { /** * String的字符串转换成unicode的String - * + * * @param strText 全角字符串 * @return String 每个unicode之间无分隔符 * @see UnicodeUtil#toUnicode(String) @@ -885,7 +885,7 @@ public class Convert { /** * unicode的String转换成String的字符串 - * + * * @param unicode Unicode符 * @return String 字符串 * @see UnicodeUtil#toString(String) @@ -897,7 +897,7 @@ public class Convert { /** * 给定字符串转换字符编码
* 如果参数为空,则返回原字符串,不报错。 - * + * * @param str 被转码的字符串 * @param sourceCharset 原字符集 * @param destCharset 目标字符集 @@ -914,7 +914,7 @@ public class Convert { /** * 转换时间单位 - * + * * @param sourceDuration 时长 * @param sourceUnit 源单位 * @param destUnit 目标单位 @@ -929,7 +929,7 @@ public class Convert { // --------------------------------------------------------------- 原始包装类型转换 /** * 原始类转为包装类,非原始类返回原类 - * + * * @see BasicType#wrap(Class) * @param clazz 原始类 * @return 包装类 @@ -941,7 +941,7 @@ public class Convert { /** * 包装类转为原始类,非包装类返回原类 - * + * * @see BasicType#unWrap(Class) * @param clazz 包装类 * @return 原始类 @@ -954,7 +954,7 @@ public class Convert { // -------------------------------------------------------------------------- 数字和英文转换 /** * 将阿拉伯数字转为英文表达方式 - * + * * @param number {@link Number}对象 * @return 英文表达式 * @since 3.0.9 @@ -962,10 +962,25 @@ public class Convert { public static String numberToWord(Number number) { return NumberWordFormatter.format(number); } - + + /** + * 将阿拉伯数字转为精简表示形式,例如: + * + *
+	 *     1200 -> 1.2k
+	 * 
+ * + * @param number {@link Number}对象 + * @return 英文表达式 + * @since 5.5.9 + */ + public static String numberToSimple(Number number) { + return NumberWordFormatter.formatSimple(number.longValue()); + } + /** * 将阿拉伯数字转为中文表达方式 - * + * * @param number 数字 * @param isUseTraditonal 是否使用繁体字(金额形式) * @return 中文 @@ -974,10 +989,10 @@ public class Convert { public static String numberToChinese(double number, boolean isUseTraditonal) { return NumberChineseFormatter.format(number, isUseTraditonal); } - + /** * 金额转为中文形式 - * + * * @param n 数字 * @return 中文大写数字 * @since 3.2.3 @@ -988,11 +1003,11 @@ public class Convert { } return NumberChineseFormatter.format(n.doubleValue(), true, true); } - + // -------------------------------------------------------------------------- 数字转换 /** * int转byte - * + * * @param intValue int值 * @return byte值 * @since 3.2.0 @@ -1003,7 +1018,7 @@ public class Convert { /** * byte转无符号int - * + * * @param byteValue byte值 * @return 无符号int值 * @since 3.2.0 @@ -1015,7 +1030,7 @@ public class Convert { /** * byte数组转short - * + * * @param bytes byte数组 * @return short值 * @since 3.2.0 @@ -1039,7 +1054,7 @@ public class Convert { /** * byte[]转int值 - * + * * @param bytes byte数组 * @return int值 * @since 3.2.0 @@ -1053,7 +1068,7 @@ public class Convert { /** * int转byte数组 - * + * * @param intValue int值 * @return byte数组 * @since 3.2.0 @@ -1070,7 +1085,7 @@ public class Convert { /** * long转byte数组
* from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java - * + * * @param longValue long值 * @return byte数组 * @since 3.2.0 @@ -1088,7 +1103,7 @@ public class Convert { /** * byte数组转long
* from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java - * + * * @param bytes byte数组 * @return long值 * @since 3.2.0 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 865bc3f93..3304a32ff 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 @@ -1,180 +1,182 @@ -package cn.hutool.core.convert; - -import cn.hutool.core.util.StrUtil; - -import java.text.DecimalFormat; - -/** - * 将浮点数类型的number转换成英语的表达方式
- * 参考博客:http://blog.csdn.net/eric_sunah/article/details/8713226 - * - * @author Looly - * @since 3.0.9 - */ -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"}; - - private static final String[] NUMBER_SUFFIX = new String[]{"k", "w", "", "m", "", "", "b", "", "", "t", "", "", "p", "", "", "e"}; - - /** - * 将阿拉伯数字转为英文表达式 - * - * @param x 阿拉伯数字,可以为{@link Number}对象,也可以是普通对象,最后会使用字符串方式处理 - * @return 英文表达式 - */ - public static String format(Object x) { - if (x != null) { - return format(x.toString()); - } else { - return ""; - } - } - - /** - * 将阿拉伯数字转化为简介计数单位,例如 2100 => 2.1k - * 范围默认只到w - * @param value - * @return - */ - public static String formatValue(long value) { - return formatValue(value, true); - } - - /** - * 将阿拉伯数字转化为简介计数单位,例如 2100 => 2.1k - * @param value 对应数字的值 - * @param isTwo 控制是否为k、w - * @return - */ - public static String formatValue(long value, boolean isTwo) { - if (value < 1000) { - return String.valueOf(value); - } - int index = -1; - double res = value * 1.0d; - while (res > 10 && (!isTwo || index < 1)) { - if (res > 1000) { - res = res / 1000; - index++; - } - if (res > 10) { - res = res / 10; - index++; - } - } - DecimalFormat decimalFormat = new DecimalFormat("#.##"); - return String.format("%s%s", decimalFormat.format(res), NUMBER_SUFFIX[index]); - } - - /** - * 将阿拉伯数字转为英文表达式 - * - * @param x 阿拉伯数字字符串 - * @return 英文表达式 - */ - private static String format(String x) { - int z = x.indexOf("."); // 取小数点位置 - String lstr, rstr = ""; - if (z > -1) { // 看是否有小数,如果有,则分别取左边和右边 - lstr = x.substring(0, z); - rstr = x.substring(z + 1); - } else { - // 否则就是全部 - lstr = x; - } - - String lstrrev = StrUtil.reverse(lstr); // 对左边的字串取反 - String[] a = new String[5]; // 定义5个字串变量来存放解析出来的叁位一组的字串 - - switch (lstrrev.length() % 3) { - case 1: - lstrrev += "00"; - break; - case 2: - lstrrev += "0"; - break; - } - 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 - // thousand only - if (i != 0) { - lm.insert(0, transThree(a[i]) + " " + parseMore(i) + " "); // 加: - // thousand、million、billion - } else { - // 防止i=0时, 在多加两个空格. - lm = new StringBuilder(transThree(a[i])); - } - } else { - lm.append(transThree(a[i])); - } - } - - String xs = ""; // 用来存放转换后小数部分 - if (z > -1) { - xs = "AND CENTS " + transTwo(rstr) + " "; // 小数部分存在时转换小数 - } - - return lm.toString().trim() + " " + xs + "ONLY"; - } - - private static String parseFirst(String s) { - return NUMBER[Integer.parseInt(s.substring(s.length() - 1))]; - } - - private static String parseTeen(String s) { - return NUMBER_TEEN[Integer.parseInt(s) - 10]; - } - - private static String parseTen(String s) { - return NUMBER_TEN[Integer.parseInt(s.substring(0, 1)) - 1]; - } - - private static String parseMore(int i) { - return NUMBER_MORE[i]; - } - - // 两位 - private static String transTwo(String s) { - String value; - // 判断位数 - if (s.length() > 2) { - s = s.substring(0, 2); - } else if (s.length() < 2) { - s = "0" + s; - } - - if (s.startsWith("0")) {// 07 - seven 是否小於10 - value = parseFirst(s); - } else if (s.startsWith("1")) {// 17 seventeen 是否在10和20之间 - value = parseTeen(s); - } else if (s.endsWith("0")) {// 是否在10与100之间的能被10整除的数 - value = parseTen(s); - } else { - value = parseTen(s) + " " + parseFirst(s); - } - return value; - } - - // 制作叁位的数 - // s.length = 3 - private static String transThree(String s) { - String value; - if (s.startsWith("0")) {// 是否小於100 - value = transTwo(s.substring(1)); - } else if ("00".equals(s.substring(1))) {// 是否被100整除 - value = parseFirst(s.substring(0, 1)) + " HUNDRED"; - } else { - value = parseFirst(s.substring(0, 1)) + " HUNDRED AND " + transTwo(s.substring(1)); - } - return value; - } -} \ No newline at end of file +package cn.hutool.core.convert; + +import cn.hutool.core.util.NumberUtil; +import cn.hutool.core.util.StrUtil; + +/** + * 将浮点数类型的number转换成英语的表达方式
+ * 参考博客:http://blog.csdn.net/eric_sunah/article/details/8713226 + * + * @author Looly,totalo + * @since 3.0.9 + */ +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"}; + + private static final String[] NUMBER_SUFFIX = new String[]{"k", "w", "", "m", "", "", "b", "", "", "t", "", "", "p", "", "", "e"}; + + /** + * 将阿拉伯数字转为英文表达式 + * + * @param x 阿拉伯数字,可以为{@link Number}对象,也可以是普通对象,最后会使用字符串方式处理 + * @return 英文表达式 + */ + public static String format(Object x) { + if (x != null) { + return format(x.toString()); + } else { + return StrUtil.EMPTY; + } + } + + /** + * 将阿拉伯数字转化为简洁计数单位,例如 2100 => 2.1k + * 范围默认只到w + * + * @param value 被格式化的数字 + * @return 格式化后的数字 + * @since 5.5.9 + */ + public static String formatSimple(long value) { + return formatSimple(value, true); + } + + /** + * 将阿拉伯数字转化为简介计数单位,例如 2100 => 2.1k + * + * @param value 对应数字的值 + * @param isTwo 控制是否为只为k、w,例如当为{@code false}时返回4.38m,{@code true}返回438.43w + * @return 格式化后的数字 + * @since 5.5.9 + */ + public static String formatSimple(long value, boolean isTwo) { + if (value < 1000) { + return String.valueOf(value); + } + int index = -1; + double res = value; + while (res > 10 && (false == isTwo || index < 1)) { + if (res > 1000) { + res = res / 1000; + index++; + } + if (res > 10) { + res = res / 10; + index++; + } + } + return String.format("%s%s", NumberUtil.decimalFormat("#.##", res), NUMBER_SUFFIX[index]); + } + + /** + * 将阿拉伯数字转为英文表达式 + * + * @param x 阿拉伯数字字符串 + * @return 英文表达式 + */ + private static String format(String x) { + int z = x.indexOf("."); // 取小数点位置 + String lstr, rstr = ""; + if (z > -1) { // 看是否有小数,如果有,则分别取左边和右边 + lstr = x.substring(0, z); + rstr = x.substring(z + 1); + } else { + // 否则就是全部 + lstr = x; + } + + String lstrrev = StrUtil.reverse(lstr); // 对左边的字串取反 + String[] a = new String[5]; // 定义5个字串变量来存放解析出来的叁位一组的字串 + + switch (lstrrev.length() % 3) { + case 1: + lstrrev += "00"; + break; + case 2: + lstrrev += "0"; + break; + } + 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 + // thousand only + if (i != 0) { + lm.insert(0, transThree(a[i]) + " " + parseMore(i) + " "); // 加: + // thousand、million、billion + } else { + // 防止i=0时, 在多加两个空格. + lm = new StringBuilder(transThree(a[i])); + } + } else { + lm.append(transThree(a[i])); + } + } + + String xs = ""; // 用来存放转换后小数部分 + if (z > -1) { + xs = "AND CENTS " + transTwo(rstr) + " "; // 小数部分存在时转换小数 + } + + return lm.toString().trim() + " " + xs + "ONLY"; + } + + private static String parseFirst(String s) { + return NUMBER[Integer.parseInt(s.substring(s.length() - 1))]; + } + + private static String parseTeen(String s) { + return NUMBER_TEEN[Integer.parseInt(s) - 10]; + } + + private static String parseTen(String s) { + return NUMBER_TEN[Integer.parseInt(s.substring(0, 1)) - 1]; + } + + private static String parseMore(int i) { + return NUMBER_MORE[i]; + } + + // 两位 + private static String transTwo(String s) { + String value; + // 判断位数 + if (s.length() > 2) { + s = s.substring(0, 2); + } else if (s.length() < 2) { + s = "0" + s; + } + + if (s.startsWith("0")) {// 07 - seven 是否小於10 + value = parseFirst(s); + } else if (s.startsWith("1")) {// 17 seventeen 是否在10和20之间 + value = parseTeen(s); + } else if (s.endsWith("0")) {// 是否在10与100之间的能被10整除的数 + value = parseTen(s); + } else { + value = parseTen(s) + " " + parseFirst(s); + } + return value; + } + + // 制作叁位的数 + // s.length = 3 + private static String transThree(String s) { + String value; + if (s.startsWith("0")) {// 是否小於100 + value = transTwo(s.substring(1)); + } else if ("00".equals(s.substring(1))) {// 是否被100整除 + value = parseFirst(s.substring(0, 1)) + " HUNDRED"; + } else { + value = parseFirst(s.substring(0, 1)) + " HUNDRED AND " + transTwo(s.substring(1)); + } + return value; + } +} 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 835b06520..629205404 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,25 +1,34 @@ -package cn.hutool.core.convert; - -import org.junit.Assert; -import org.junit.Test; - -public class NumberWordFormatTest { - - @Test - public void formatTest() { - String format = NumberWordFormatter.format(100.23); - Assert.assertEquals("ONE HUNDRED AND CENTS TWENTY THREE ONLY", format); - - String format2 = NumberWordFormatter.format("2100.00"); - Assert.assertEquals("TWO THOUSAND ONE HUNDRED AND CENTS ONLY", format2); - - String format3 = NumberWordFormatter.formatValue(4384324, false); - Assert.assertEquals("4.38m", format3); - - String format4 = NumberWordFormatter.formatValue(4384324); - Assert.assertEquals("438.43w", format4); - - String format5 = NumberWordFormatter.formatValue(438); - Assert.assertEquals("438", format5); - } -} +package cn.hutool.core.convert; + +import org.junit.Assert; +import org.junit.Test; + +public class NumberWordFormatTest { + + @Test + public void formatTest() { + String format = NumberWordFormatter.format(100.23); + Assert.assertEquals("ONE HUNDRED AND CENTS TWENTY THREE ONLY", format); + + String format2 = NumberWordFormatter.format("2100.00"); + Assert.assertEquals("TWO THOUSAND ONE HUNDRED AND CENTS ONLY", format2); + } + + @Test + public void formatSimpleTest() { + String format1 = NumberWordFormatter.formatSimple(1200, false); + Assert.assertEquals("1.2k", format1); + + 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); + + String format5 = NumberWordFormatter.formatSimple(438); + Assert.assertEquals("438", format5); + } +}