sql server - Use Float or Decimal for Accounting Application Dollar Amount? -
we rewriting our legacy accounting system in vb.net , sql server. brought in new team of .net/ sql programmers rewrite. of system completed dollar amounts using floats. legacy system language, programmed in, did not have float have used decimal.
what recommendation?
should float or decimal data type used dollar amounts?
what of pros , cons either?
one con mentioned in our daily scrum have careful when calculate amount returns result on 2 decimal positions. sounds have round amount 2 decimal positions.
another con displays , printed amounts have have format statement shows 2 decimal positions. noticed few times not done , amounts did not correct. (i.e. 10.2 or 10.2546)
a pro float takes 8 bytes on disk decimal take 9 bytes (decimal 12,2)
should float or decimal data type used dollar amounts?
the answer easy. never floats. never !
floats according ieee 754 binary, new standard ieee 754r defined decimal formats. many of fractional binary parts can never equal exact decimal representation. binary number can written m/2^n (m, n positive integers), decimal number m/(2^n*5^n). binaries lack prime factor 5, binary numbers can represented decimals, 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 end number either higher or lower given decimal number. always.
why matter ? rounding. normal rounding means 0..4 down, 5..9 up. does matter if result either 0.049999999999.... or 0.0500000000... may know means 5 cent, computer not know , rounds 0.4999... down (wrong) , 0.5000... (right). given result of floating point computations contain small error terms, decision pure luck. gets hopeless if want decimal round-to-even handling binary numbers.
unconvinced ? insist in account system ok ? assets , liabilities equal? ok, take each of given formatted numbers of each entry, parse them , sum them independent decimal system ! compare formatted sum. oops, there wrong, isn't ?
for calculation, extreme accuracy , fidelity required (we used oracle's float) record "billionth's of penny" being accrued.
doesn't against error. because people automatically assume computer sums right, practically no 1 checks independently.
Comments
Post a Comment