decimal - Ideal data type for currency rate in MySQL -


what should ideally use store currency rate information in mysql database? should use:

  • double
  • decimal (10,4)
  • float or
  • something else?

currency rates decimal numbers 1.2362.

how precise need be? ask because problem may go beyond storage format of conversion factor. example, perhaps rules might keep factor n decimal places, perform multiplication (or division) m places, round. , "round" might "financial" -- 0.5 rounds up, instead of ieee-754, "round nearest even".

assuming can afford off 1 unit in last place (cents or centimes or whatever), use double.

i assume won't storing more than, say, 100 conversion factors, fact double occupies 8 bytes not issue. if recording conversion factors every minute past 10 years, rethink decision.

float gives 6-7 significant digits (and takes 4 bytes); double gives 16. 12345 , 123.45 , 0.00012345 each have 5 "significant" digits.

for currency, itself, think maximum number of decimal places currency today 4. is, decimal(nn, 4) should suffice storing money.

however, not provide necessary rounding.

round(currency1 * factor, decimals) 

where decimals 2 target currency of usd , euros (and many other currencies). pointed out above, may differ bank compute as 1 cent.

(would call "opinion based"? or better?)


Comments