mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add decimalFormat
This commit is contained in:
parent
d623d6925c
commit
0665c4ed44
@ -7,6 +7,7 @@
|
||||
|
||||
### 🐣新特性
|
||||
* 【http 】 HttpUtil增加closeCookie方法
|
||||
* 【core 】 NumberUtil增加方法decimalFormat重载(issue#I3OSA2@Gitee)
|
||||
### 🐞Bug修复
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
@ -1054,7 +1054,35 @@ public class NumberUtil {
|
||||
* @since 5.1.6
|
||||
*/
|
||||
public static String decimalFormat(String pattern, Object value) {
|
||||
return new DecimalFormat(pattern).format(value);
|
||||
return decimalFormat(pattern, value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化double<br>
|
||||
* 对 {@link DecimalFormat} 做封装<br>
|
||||
*
|
||||
* @param pattern 格式 格式中主要以 # 和 0 两种占位符号来指定数字长度。0 表示如果位数不足则以 0 填充,# 表示只要有可能就把数字拉上这个位置。<br>
|
||||
* <ul>
|
||||
* <li>0 =》 取一位整数</li>
|
||||
* <li>0.00 =》 取一位整数和两位小数</li>
|
||||
* <li>00.000 =》 取两位整数和三位小数</li>
|
||||
* <li># =》 取所有整数部分</li>
|
||||
* <li>#.##% =》 以百分比方式计数,并取两位小数</li>
|
||||
* <li>#.#####E0 =》 显示为科学计数法,并取五位小数</li>
|
||||
* <li>,### =》 每三位以逗号进行分隔,例如:299,792,458</li>
|
||||
* <li>光速大小为每秒,###米 =》 将格式嵌入文本</li>
|
||||
* </ul>
|
||||
* @param value 值,支持BigDecimal、BigInteger、Number等类型
|
||||
* @param roundingMode 保留小数的方式枚举
|
||||
* @return 格式化后的值
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static String decimalFormat(String pattern, Object value, RoundingMode roundingMode) {
|
||||
final DecimalFormat decimalFormat = new DecimalFormat(pattern);
|
||||
if(null != roundingMode){
|
||||
decimalFormat.setRoundingMode(roundingMode);
|
||||
}
|
||||
return decimalFormat.format(roundingMode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user