我有这样的方法:
try
{
PreparedStatement pst = null;
..............................
while (rs.next()) {
money = rs.getDouble("money");
System.out.println(money);
}
} catch (Exception e) {
System.err.println(e);
}
如何正确打印双倍的货币价值?因为当我打印钱时,我看到这样的东西:250.0但在数据库中我的价值是250.00
答案 0 :(得分:3)
使用double
时,您可能会失去精确度,这在处理currency
时很糟糕。请改用BigDecimal
while (rs.next()) {
BigDecimal money = rs.getBigDecimal("money");
//if needed set it's scale to something else
//money=money.setScale(2,RoundingMode.HALF_UP); //2 decimals rounded up
System.out.println(money);
}