Re: Division

From: Robert Ftizpatrick <robert(at)webtent(dot)com>
To: Tino Wildenhain <tino(at)wildenhain(dot)de>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Division
Date: 2005-11-04 17:48:18
Message-ID: 1131126498.22487.61.camel@columbus.webtent.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 2005-11-04 at 18:39 +0100, Tino Wildenhain wrote:
> > CREATE OR REPLACE FUNCTION "public"."issue" (varchar) RETURNS numeric
> > AS'
> > DECLARE
> > repar text[];
> > pct numeric(3,2);
> > noreps integer;
> >
> > BEGIN
> > repar := string_to_array($1,''-'');
> > noreps := array_upper(repar,1);
> > pct := 1/noreps;
>
> you have noreps integer, 1 is integer too so division almost
> every time will result in 0. (Unless noreps is 0)
> Then 0 will be casted to numeric(3,2) which is 0.00.
>
> > RETURN pct;
> > END;
> > 'LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
> >
> > bachman=# select issue('AA-BB');
> > issue
> > -------
> > 0.00
> > (1 row)
>
> Btw, apart from the integer problem you are facing, what is
> the whole point of that function (when it finally 'works')?
>

Thanks, of course, can't see for the confusion :(

It is part of a trigger that breaks apart an incoming CSV field by
dashes into an array and inserts the percent of the transaction that
belongs to each element of the array. So, if there are 2 elements, each
get 50%, if 4, then they will get 25%, so on.

Thanks again...

--
Robert

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Filipov 2005-11-04 18:08:43 I just can't get it:record_in, record_out(I know i try to do something postgres is not developed for but there are many 'buts')
Previous Message Peter Eisentraut 2005-11-04 17:46:02 Re: Division