RE: Monetary field in MySQL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> >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 `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/



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux