Re: simple division

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Martin Mueller <martinmueller(at)northwestern(dot)edu>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: simple division
Date: 2018-12-04 20:42:29
Message-ID: CAKFQuwYp1CJ_Aux0jSUQ_TsJ0zsPxQOEkWZp3kSyDmQZ0ZVNJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Dec 4, 2018 at 1:29 PM Martin Mueller
<martinmueller(at)northwestern(dot)edu> wrote:
> I have asked this question before and apologize for not remembering it. How do you do simple division in postgres and get 10/4 with decimals?
> This involves cast and numeric in odd ways that are not well explained in the documentation. For instance, you’d expect an example in the Mathematical Functions. But there isn’t.

select 10/4, 10.0/4, 10/4.0, 10.0/4.0;

The first one returns 2, the rest of them 2.5 - from which one can
infer that if both inputs are integer (type) the output is integer
(type) - if at least one input is non-integer (type) the output will
be as well.

If you want to cast...select 10/(4::numeric)...

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2018-12-04 20:45:02 Re: simple division
Previous Message Thomas Kellerer 2018-12-04 20:41:51 Re: simple division