This commit is contained in:
Looly 2022-02-11 13:39:40 +08:00
commit dd716482e2
5 changed files with 54 additions and 1 deletions

View File

@ -25,6 +25,7 @@
* 【system 】 修复CpuInfo.getUsed()方法issue#2116@Github
* 【dfa 】 修复密集匹配和贪婪匹配冲突问题issue#2126@Github
* 【db 】 修复c3p0丢失信息问题issue#I4T7XZ@Gitee
* 【http 】 修复Action中HttpExchange没有关闭问题
-------------------------------------------------------------------------------------------------------------
# 5.7.20 (2022-01-20)

View File

@ -2,6 +2,7 @@ package cn.hutool.core.convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
/**
@ -153,6 +154,27 @@ public class NumberChineseFormatter {
return chineseStr.toString();
}
/**
* 阿拉伯数字支持正负整数四舍五入后转换成中文节权位简洁计数单位例如 -5_5555 = -5.56万
*
* @param amount 数字
* @return 中文
*/
public static String formatSimple(long amount) {
if (amount < 1_0000 && amount > -1_0000) {
return String.valueOf(amount);
}
String res;
if (amount < 1_0000_0000 && amount > -1_0000_0000) {
res = NumberUtil.div(amount, 1_0000, 2) + "";
} else if (amount < 1_0000_0000_0000L && amount > -1_0000_0000_0000L) {
res = NumberUtil.div(amount, 1_0000_0000, 2) + "亿";
} else {
res = NumberUtil.div(amount, 1_0000_0000_0000L, 2) + "万亿";
}
return res;
}
/**
* 格式化-999~999之间的数字<br>
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"

View File

@ -175,6 +175,26 @@ public class NumberChineseFormatterTest {
Assert.assertEquals("零点零伍", f1);
}
@Test
public void formatSimpleTest() {
String f1 = NumberChineseFormatter.formatSimple(1_2345);
Assert.assertEquals("1.23万", f1);
f1 = NumberChineseFormatter.formatSimple(-5_5555);
Assert.assertEquals("-5.56万", f1);
f1 = NumberChineseFormatter.formatSimple(1_2345_6789);
Assert.assertEquals("1.23亿", f1);
f1 = NumberChineseFormatter.formatSimple(-5_5555_5555);
Assert.assertEquals("-5.56亿", f1);
f1 = NumberChineseFormatter.formatSimple(1_2345_6789_1011L);
Assert.assertEquals("1.23万亿", f1);
f1 = NumberChineseFormatter.formatSimple(-5_5555_5555_5555L);
Assert.assertEquals("-5.56万亿", f1);
f1 = NumberChineseFormatter.formatSimple(123);
Assert.assertEquals("123", f1);
f1 = NumberChineseFormatter.formatSimple(-123);
Assert.assertEquals("-123", f1);
}
@Test
public void digitToChineseTest() {
String digitToChinese = Convert.digitToChinese(12_4124_1241_2421.12);

View File

@ -4,6 +4,7 @@ import cn.hutool.core.util.CharsetUtil;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import java.io.Closeable;
import java.nio.charset.Charset;
/**
@ -12,7 +13,7 @@ import java.nio.charset.Charset;
* @author looly
* @since 5.2.6
*/
public class HttpServerBase {
public class HttpServerBase implements Closeable {
final static Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8;
@ -45,4 +46,12 @@ public class HttpServerBase {
public HttpContext getHttpContext() {
return getHttpExchange().getHttpContext();
}
/**
* 调用{@link HttpExchange#close()}关闭请求流和响应流
*/
@Override
public void close() {
this.httpExchange.close();
}
}

View File

@ -33,5 +33,6 @@ public class ActionHandler implements HttpHandler {
new HttpServerRequest(httpExchange),
new HttpServerResponse(httpExchange)
);
httpExchange.close();
}
}