Alex Rivera | Logout

Float or decimal for prices?

Asked 2010-09-22T11:24:48.117
30

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

Edit
Report

1 Answer

25

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!

answered 2010-09-22T11:54:11.497

Your Answer