Re: How to sum monetary variables

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

 



On Mon, Jul 18, 2011 at 11:22:00PM +0100, Tim Streater wrote:

> On 18 Jul 2011 at 23:00, Martín Marqués <martin.marques@xxxxxxxxx> wrote: 
> 
> > 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.
> 
> Much safer to price everything internally in pence or cents or whatever, and convert to £xxx.xx for external display. Then just use integer arithmetic for the calculations.

Let me echo this. If you need precise arithmetic in an environment where
you're doing mostly adds and subtracts, store data as integers and do
your math that way. Only convert to decimal for display. If you're doing
multiplies and divides, do them with the multi-precision PHP functions.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux