[Db] [Mysql] Decimal or Double

Marcello Vezzelli marcello a vezz.it
Mar 20 Feb 2007 18:20:29 CET


Domenico L. ha scritto:
> Ciao all,
> avrei il bisogno gestire con precisione numeri molto grossi (miliardi) 
> oppure molto piccoli.
>
> Tutto nella stessa tabella, però non mi capita mai che ho grossi 
> numeri con molti decimali, in pratica posso considerare che devo 
> memorizzare al più 12 cifre, ma non so esattamente dove si posizione 
> la virgola.
>
> Qual'è la soluzione migliore (se esiste), se uso un double dovrei fare 
> una cosa tipo double(22,8), se uso il decimal? idem?

Dal manuale online:

|DOUBLE[(/|M|/,/|D|/)] [UNSIGNED] [ZEROFILL]|

A normal-size (double-precision) floating-point number. Allowable values 
are |-1.7976931348623157E+308| to |-2.2250738585072014E-308|, |0|, and 
|2.2250738585072014E-308| to |1.7976931348623157E+308|. These are the 
theoretical limits, based on the IEEE standard. The actual range might 
be slightly smaller depending on your hardware or operating system.

/|M|/ is the total number of digits and /|D|/ is the number of digits 
following the decimal point. If /|M|/ and /|D|/ are omitted, values are 
stored to the limits allowed by the hardware. A double-precision 
floating-point number is accurate to approximately 15 decimal places.


|DECIMAL[(/|M|/[,/|D|/])] [UNSIGNED] [ZEROFILL]|

For MySQL 5.0.3 and above:

A packed “exact” fixed-point number. /|M|/ is the total number of digits 
(the precision) and /|D|/ is the number of digits after the decimal 
point (the scale). The decimal point and (for negative numbers) the 
‘|-|’ sign are not counted in /|M|/. If /|D|/ is 0, values have no 
decimal point or fractional part. The maximum number of digits (/|M|/) 
for |DECIMAL| is 65 (64 from 5.0.3 to 5.0.5). The maximum number of 
supported decimals (/|D|/) is 30. If /|D|/ is omitted, the default is 0. 
If /|M|/ is omitted, the default is 10.


Se parli di miliardi (1.000.000.000) e di milionesimi (0.000000001) , 
secondo me è sufficiente il double. Dovrebbe essere anche più 
prestazionale nei calcoli.
Occhio pero' agli arrotondamenti... potresti trovarti con dei "rotti" 
dovuti al formato di memorizzazione, cose del tipo 0.00000000100023 se 
memorizzi 0.0000000001 (esempio inventato). Puoi gestire la cosa con un 
round alla precisione che vuoi tipo così (round(valore * 
1000000000)/1000000000)
Se vuoi evitare questo problema, usa decimal.

Saluti
Marcello





Maggiori informazioni sulla lista Db