Float or decimal for prices?

前端 未结 5 1402
误落风尘
误落风尘 2020-12-28 17:49

Which type (Float or decimal) is best used to store prices in a mysql database?

5条回答
  •  遥遥无期
    2020-12-28 18:35

    Prices are decimal values, and calculations on them are expected to behave like decimal fractions when it comes to rounding, literals, etc.

    That's exactly what decimal types do.

    Floats are stored as binary fractions, and they do not behave like decimal fractions - their behaviour is frequently not what people used to decimal math expect. Read The Floating-Point Guide for detailed explanations.

    For money values, never never use binary float types - especially when you have a perfectly good decimal type available!

提交回复
热议问题