An easy to use MySQL access library for converting numbers and strings to currency amounts and back, avoiding floating point errors.

If you're developing a web application with PHP and money is involved, you may be interested in this library.

To safely store currency amounts in a database that doesn't have a specific datatype for currency, you need to store them as integers. So just count the cents instead of dollars with decimals:

$cents = toCents("5.35"); // 535
$cents = toCents(8.24); // 824

When you read cents out of the database and you want to display it as a formatted currency amount to the user, use the companion function:

$money = fromCents(535); // "5.35"
$money = fromCents(824); // "8.24"

Now you can safely multiply, divide, and add dollar amounts using database functions.


Get the source: money.php.txt