在PreparedStatement中打印双倍

时间:2014-12-19 10:05:34

标签: java mysql

我有这样的方法:

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

1 个答案:

答案 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);             
}