RE: [PHP-DB] Monetary field in MySQL
| From: | John W. Holmes | Date: | Fri, 23 May 2003 16:15:51 +0000 |
| Subject: | RE: [PHP-DB] Monetary field in MySQL | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-27995@lists.php.net to get a copy of this message | ||
> >how can I have a field in MySQL that represents a monetary value i.e.
it
> >always has 2 zeros after the decimal point?
>
> you can use a float type.
>
> ALTER TABLE
blah ADD TA- ùlD
> i[ñQ>âaMoneyField FLOAT(6,2) DEFAULT "0.00"
You should use a DECIMAL column as you won't run into floating point
error problems. Even though you say FLOAT(6,2), that's just precision.
$12.25 may be stored in the database as 12.250000001 and meet the
precision you gave. However, now if you're looking for a price ==
$12.25, this row will not be returned. DECIMAL columns are actually
stored as strings, so there is no floating point errors. $12.25 is
stored as "12.25" and there are no problems.
Oh, and the obvious, this is not a PHP question!!
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/