java数字精度格式化

阅读更多
private BigDecimal formatComma2BigDecimal(Object obj) {
String val = String.valueOf(obj);
if (val == null)
return new BigDecimal("0.00");

val = val.replaceAll(",", "");
if (!isNumber(val))
return new BigDecimal("0.00");

BigDecimal decimal = new BigDecimal(val);
decimal = decimal.setScale(2, RoundingMode.HALF_UP);

return decimal;

}
private String formatCommaAnd2Point(Object obj) {
BigDecimal decimal = formatComma2BigDecimal(obj);

DecimalFormat df = new DecimalFormat("#,###.00");
String decimalStr = df.format(decimal).equals(".00")?"0.00":df.format(decimal);
if(decimalStr.startsWith(".")){
decimalStr = "0"+decimalStr;
}
return decimalStr;
}
private boolean isDouble(String value) {
try {
Double.parseDouble(value);
if (value.contains("."))
return true;
return false;
} catch (NumberFormatException e) {
return false;
}
}
private boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
private boolean isNumber(String value) {
return isInteger(value) || isDouble(value);
}

你可能感兴趣的:(java数字精度格式化,java)