Which PostgreSQL column type should be used to store a Java BigDecimal?

后端 未结 3 2036
挽巷
挽巷 2021-01-03 20:15

What PostgreSQL column type should I use to store a Java BigDecimal?

相关标签:
3条回答
  • 2021-01-03 20:48

    Just use the Java mappings for the common SQL data types. In this case you can use a NUMERIC or DECIMAL.

    0 讨论(0)
  • 2021-01-03 20:53

    I'd use decimal adding my own scale and precision:

    t.decimal "price", precision: 12, scale: 4, default: "0.0"
    
    0 讨论(0)
  • 2021-01-03 20:55

    See PostgreSQL datatypes - perhaps Numeric, which can act as an arbitrary precision type (this is a PostgreSQL extension).

    ...without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision.

    I am not entirely sure what "implementation limit on precision is", though. Have never tried really large numbers. If the limit is reached, then a fallback to text is possible. But, I suspect there will be significant other issues before then ;-) If you wish to store less, then specify a precision and/or scale to numeric.

    Edit as sjr pointed out, the restriction is 1000 decimal digits of precision (from the same link):

    The type numeric can store numbers with up to 1000 digits of precision [in currentl implementations] and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required...

    If more precision is needed - despite having a much bigger problem at hand - then a numeric column will not be suitable (by itself). But this is really more of a very extreme "what if" and likely does not play a limiting role.

    0 讨论(0)
提交回复
热议问题