Can't put Double number in BigDecimal variable

后端 未结 2 667
旧巷少年郎
旧巷少年郎 2021-01-27 10:20

I\'m using a Double variable which holds the Item price. That variable is stored in postgresql database under a column of money type. I use setBigDecimal(position,value) SQL fu

相关标签:
2条回答
  • 2021-01-27 10:26

    This program illustrates conversion in both directions between Double and BigDecimal:

    import java.math.BigDecimal;
    
    public class Test {
      public static void main(String[] args) {
        Double d1 = 1.3;
        BigDecimal bd1 = BigDecimal.valueOf(d1.doubleValue());
        Double d2 = bd1.doubleValue();
        System.out.println(d2);
      }
    }
    

    Note that the conversion to Double may not be exact.

    0 讨论(0)
  • 2021-01-27 10:36

    Obviously priceSpinner.getValue() returns BigDecimal and you're trying to convert it to double and then back to BigDecimal.

    Why don't you just do?

    insertStmt.setBigDecimal(position, (BigDecimal) priceSpinner.getValue());
    
    0 讨论(0)
提交回复
热议问题