Re: How to sum monetary variables
| From: | Adam Richardson | Date: | Mon, 18 Jul 2011 22:30:38 +0000 |
| Subject: | Re: How to sum monetary variables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314105@lists.php.net to get a copy of this message | ||
2011/7/18 Martín Marqués <martin.marques@gmail.com>
> I'm building a table (which is a report that has to be printed) with a
> bunch of items (up to 300 in some cases) that have unitary price
> (stored in a numeric(9,2) field), how many there are, and the total
> price for each item. At the end of the table there is a total of all
> the items.
>
> The app is running on PHP and PostgreSQL is the backend.
>
> The question is, how do I get the total of everything?
>
> Running it on PHP gives one value, doing a sum() on the backend gives
> another, and I'm starting to notice that even using python as a
> calculator gives me errors (big ones). Right now I'm doing the maths
> by hand to find out who has the biggest error, or if any is 100%
> accurate.
>
> Any ideas?
>
Hi,
I've not had issues with PostgreSQL when using the numeric data type. That
said, when you need more precision than PHP's standard handling of floating
points (http://php.net/manual/en/language.types.float.php), you can use
PHP's BC Math functions to enforce arbitrary precision:
http://www.php.net/manual/en/ref.bc.php
<http://www.php.net/manual/en/ref.bc.php>
Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com