How can I store a BigDecimal (Java) value in a Real (SQlite) column?

前端 未结 3 2212
星月不相逢
星月不相逢 2020-12-17 04:09

I\'m still having big problems with BigDecimal (the trail of tears started here, and continued to here so far.

Now I\'ve got the opposite problem - going from BigDec

3条回答
  •  孤城傲影
    2020-12-17 04:38

    I use the SQLite Integer type and convert like so:

    BigDecimal bd = new BigDecimal("1234.5678");
    
    int packedInt = bd.scaleByPowerOfTen(4).intValue(); // packedInt now = 12345678
    

    Now save packedInt to SQLite Integer field.

    To go back to BigDecimal:

    BigDecimal bd = new BigDecimal(packedInt); // bd = 12345678
    
    bd = bd.scaleByPowerOfTen(-4);             // now bd = 1234.5678
    

提交回复
热议问题