Float or decimal for prices?

前端 未结 5 1410
误落风尘
误落风尘 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:46

    For Financial calculations use Decimal

    According to IEEE 754 Floats were always binary, only the new standard IEEE 754R defined decimal formats. Many of the fractional binary parts can never equal the exact decimal representation. Any binary number can be written as m/2^n (m, n positive integers), any decimal number as m/(2^n*5^n). As binarys lack the prime factor 5, all binary numbers can be exactly represented by decimals, but not vice versa.

    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 calculations use Decimal not FLOAT

提交回复
热议问题