[PHP] How to sum monetary variables

2011-07-18 Thread Martín Marqués
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?

-- 
Martín Marqués
select 'martin.marques' || '@' || 'gmail.com'
DBA, Programador, Administrador

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



RE: [PHP] How to sum monetary variables

2011-07-18 Thread Jasper Mulder

> Date: Mon, 18 Jul 2011 19:00:52 -0300
> From: martin.marq...@gmail.com
> To: php-general@lists.php.net
> Subject: [PHP] How to sum monetary variables
> 
> 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?

According to the postgreSQL docs, there might occur an error as the sum()
 output is coerced to have a 9 digit precision (so at most 999,99 as a 
value), and as this is different from the PHP float interpretation, they might
yield different results in case of overflow. However as python supports
arbitrary integer arithmetic, overflows should not occur.

At the moment, still overflow errors seem the most likely explanation. Does
your table consist of very large values (occasionally perhaps)?

Could you give us an example?

Best regards,
Jasper
  

Re: [PHP] How to sum monetary variables

2011-07-18 Thread Tim Streater
On 18 Jul 2011 at 23:00, Martín Marqués  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.

--
Cheers  --  Tim

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

Re: [PHP] How to sum monetary variables

2011-07-18 Thread Adam Richardson
2011/7/18 Martín Marqués 

> 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

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


RE: [PHP] How to sum monetary variables

2011-07-18 Thread admin
> -Original Message-
> From: Martín Marqués [mailto:martin.marq...@gmail.com]
> Sent: Monday, July 18, 2011 6:01 PM
> To: PHP General
> Subject: [PHP] How to sum monetary variables
> 
> 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?
> 
> --
> Martín Marqués
> select 'martin.marques' || '@' || 'gmail.com'
> DBA, Programador, Administrador
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php



Can you show us some examples of what you have tried please.

Richard L. Buskirk


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



Re: [PHP] How to sum monetary variables

2011-07-18 Thread Richard Quadling
2011/7/18 Martín Marqués :
> 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?

For financial values, I use the money type.

I use MS SQL, but PostgreSQL has
http://www.postgresql.org/docs/9.0/interactive/datatype-money.html


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

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



Re: [PHP] How to sum monetary variables

2011-07-18 Thread Adam Richardson
2011/7/18 Richard Quadling 

> 2011/7/18 Martín Marqués :
> >
> > Any ideas?
>
> For financial values, I use the money type.
>
> I use MS SQL, but PostgreSQL has
> http://www.postgresql.org/docs/9.0/interactive/datatype-money.html
>

The version of PostgreSQL plays a role, too, as at one point the money type
was deprecated (and I still tend to use numeric, even though work has been
done to improve the money type):
http://archives.postgresql.org/pgsql-general/2008-05/msg00979.php
http://www.postgresql.org/docs/8.2/static/datatype-money.html

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


Re: [PHP] How to sum monetary variables

2011-07-18 Thread Paul M Foster
On Mon, Jul 18, 2011 at 11:22:00PM +0100, Tim Streater wrote:

> On 18 Jul 2011 at 23:00, Martín Marqués  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



[PHP] default option

2011-07-18 Thread Chris Stinemetz
Hello,

I am building some select menu's dynamically from a mysql database and
am courous how to give the menu a default option "Choose".

Below is what I have so far for one of my menu's.

Thank you in advace.


 $value)
{
$selected = '';
if($value == $market)
{
$selected = 'selected';
}
echo("$value : $market_name[$key]");
}
?>


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



Re: [PHP] default option

2011-07-18 Thread Stuart Dallas
On Tue, Jul 19, 2011 at 4:28 AM, Chris Stinemetz
wrote:

> Hello,
>
> I am building some select menu's dynamically from a mysql database and
> am courous how to give the menu a default option "Choose".
>
> Below is what I have so far for one of my menu's.
>
> Thank you in advace.
>
> onchange="javascript:get(this.parentNode);">
>foreach($market_prefix as $key => $value)
>{
>$selected = '';
>if($value == $market)
>{
>$selected = 'selected';
>}
>echo(" >$value : $market_name[$key]");
>}
>?>
>


Just add it as the first option. If none of the options have the selected
attribute, the first option will be the selected one.

I'd also recommend you escape the variables you're outputting, on the off
chance they contain HTML-like code.


  Choose...
 $value)
  {
$selected = '';
if ($value == $market)
{
  $selected = 'selected';
}
echo '',
htmlspecialchars($value.' : '.$market_name[$key]), '';
  }
?>


-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


Re: [PHP] default option

2011-07-18 Thread Chris Stinemetz
Thank you Stuart. That did the trick.

On Mon, Jul 18, 2011 at 10:38 PM, Stuart Dallas  wrote:
> On Tue, Jul 19, 2011 at 4:28 AM, Chris Stinemetz 
> wrote:
>>
>> Hello,
>>
>> I am building some select menu's dynamically from a mysql database and
>> am courous how to give the menu a default option "Choose".
>>
>> Below is what I have so far for one of my menu's.
>>
>> Thank you in advace.
>>
>>                    > onchange="javascript:get(this.parentNode);">
>>                    >                        foreach($market_prefix as $key => $value)
>>                            {
>>                            $selected = '';
>>                            if($value == $market)
>>                            {
>>                            $selected = 'selected';
>>                            }
>>                            echo("> >$value : $market_name[$key]");
>>                            }
>>                            ?>
>>                    
>
> Just add it as the first option. If none of the options have the selected
> attribute, the first option will be the selected one.
> I'd also recommend you escape the variables you're outputting, on the off
> chance they contain HTML-like code.
> 
>   Choose...
>    foreach($market_prefix as $key => $value)
>   {
>     $selected = '';
>     if ($value == $market)
>     {
>       $selected = 'selected';
>     }
>     echo '',
> htmlspecialchars($value.' : '.$market_name[$key]), '';
>   }
> ?>
> 
> -Stuart
>
> --
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/

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