工作中遇到BigDecimal格式化问题,手中无jdk手册,只好百度一下,这种问题,用百度还是找得到滴。
解决如下:
/**
* Fortmat BigDecimal while the number is too long.
* for example: the param value = 1222222.222222,
* and then the return will be 1,222,222.22 . It depends on
NumberFormat.getInstance() .
* @param value
* @return
*/
public String formatValue(Object value){
String content = null;
if (value == null) {
content = "";
} else {
if(value instanceof BigDecimal){
//conver to fortmat String
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
content = nf.format(value);
}else{
content = String.valueOf(value);
}
}
return content;
}
BigDecimal格式化技巧
本文介绍了一种使用NumberFormat处理BigDecimal过长数值的方法,确保小数点后保留两位,并且使用了逗号作为千位分隔符。
1134

被折叠的 条评论
为什么被折叠?



