# Float or decimal for prices?

Which sort (Float or decimal) is better accustomed to store prices inside a mysql database?

Thx

Floats aren't exact and may introduce cumulative rounding errors. Decimal is the greatest format for financial information that must definitely be exact.

For Financial information use Decimal

Based on IEEE 754 Floats were always binary, just the new standard IEEE 754R defined decimal formats. Most of the fractional binary parts can't ever equal the precise decimal representation. Any binary number could be written as m/2^n (m, n positive integers), any decimal number as m/(2^n*5^n). As binarys don't have the prime factor 5, all binary amounts could be exactly symbolized by decimals, although not the other way around.

``````0.3 = 3/(2^1 * 5^1) = 0.3

0.3 = [0.25/0.5] [0.25/0.375] [0.25/3.125] [0.2825/3.125]

1/4       1/8     1/16       1/32
``````

So for Financial information use Decimal not FLOAT

Costs are decimal values, and information in it are required some thing like decimal fractions if this involves rounding, literals, etc.

That is what decimal types do.

Floats are saved as binary fractions, plus they do not become decimal fractions - their behavior is often not what individuals accustomed to decimal math expect. Read The Floating-Point Guide for detailed explanations.

For the money values, never never use binary float types - especially if you have a wonderfully good decimal type available!

Whenever we store several in float we do not save the precise number,it's an approximation. The integer part will get the priority and fractional part is really as close because the type size. If you have information and want accurate result use Decimal.

Please Use BigDecimal , because it is right for the costs , since pennies are rounded correctly to dollar.

Joshua Bloch suggests BigDecimal.