Re: dividing integers not producing decimal fractions

From: rloefgren(at)forethought(dot)net
To: pgsql-general(at)postgresql(dot)org
Subject: Re: dividing integers not producing decimal fractions
Date: 2006-11-03 22:03:59
Message-ID: 1162591439.194691.85340@e3g2000cwe.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alexander Staubo wrote:
> On Nov 2, 2006, at 23:54 , rloefgren(at)forethought(dot)net wrote:
>
> > I'm trying to produce a decimal fraction by dividing integer fields
> > like this fragment of the query:
> >
> > ...((cntoh0.count + cntoh1.count)/ttl_deptcat.ttlcount) as "Ratio"...
> ...
> > How do I get fractions when dividing integers? Cast as something?
>
> Exactly. Just as in most computer languages, incidentally:
>
> # select 1 / 2 as x;
> x
> ---
> 0
> (1 row)
>
> # select 1 / 2::float as x;
> x
> -----
> 0.5
> (1 row)
>
> Alexander.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq

You're right (I dug around in the documentation and edjoocated myself).
However:

sales=# select 1/2;
?column?
----------
0
(1 row)

and:

sales=# select 1/2::float;
?column?
----------
0.5
(1 row)

but:

sales=# select 1/2*4::float;
?column?
----------
0
(1 row)

or:

sales=# select (1/2)*4::float;
?column?
----------
0
(1 row)

or try:

sales=# select (1/2)* 4::numeric(6,3);
?column?
----------
0.000
(1 row)

I'll just avoid this next time by not throwing "integer" around so
quickly. (At least until I become something of a postgres obi-wan...)

thanks,

r

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Staubo 2006-11-03 22:08:47 Per-row security
Previous Message Andreas Kretschmer 2006-11-03 21:49:51 Re: Odd dump/restore question