PostgreSQL: Which Datatype should be used for Currency?

前端 未结 6 816
盖世英雄少女心
盖世英雄少女心 2020-11-27 10:56

Seems like Money type is discouraged as described here

My application needs to store currency, which datatype shall I be using? Numeric, Money or FLOAT?

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 11:49

    Use a 64-bit integer stored as bigint

    I recommend using micro-dollars (or similar major currency). Micro means 1 millionth so 1 micro-dollar = $0.000001.

    • Simple to use and compatible with every language.
    • Enough precision to handle fractions of a cent.
    • Works for very small per-unit pricing (like ad impressions or API charges).
    • Smaller data size for storage than strings or numerics.
    • Easy to maintain accuracy through calculations and apply rounding at the final output.

提交回复
热议问题