Обсуждение: a very primitive question about division

Поиск
Список
Период
Сортировка

a very primitive question about division

От
Martin Mueller
Дата:

I have a very primitive question about division to which I can’t find an obvious answer in the documentation.

 

Given two values defined as integers, how do I divide one by the other and get an answer with two decimals, e.g 3 /4 = 0.75.

 

This is very simple in mysql and seems to be oddly contorted in postgres. I spent half an hour on the Web and with the documentation but couldn’t find a simple example.

 

Probably my stupidity.

Re: a very primitive question about division

От
"David G. Johnston"
Дата:
On Wed, Mar 7, 2018 at 10:21 AM, Martin Mueller <martinmueller@northwestern.edu> wrote:

Given two values defined as integers, how do I divide one by the other and get an answer with two decimals, e.g 3 /4 = 0.75.


​Case one of them to numeric.

​select 3/4::numeric
​David J.​

Re: a very primitive question about division

От
Martin Mueller
Дата:

Thanks.  So  round(before1550/colfreq::numeric, 2)  produces the desired result. 

 

The explanations and examples of string functions in the Postgres documentation are a model of clarity. The explanations and examples of basic arithmetic operations  are not.  There is room for improvement there.

 

From: "David G. Johnston" <david.g.johnston@gmail.com>
Date: Wednesday, March 7, 2018 at 11:23 AM
To: Martin Mueller <martinmueller@northwestern.edu>
Cc: "pgsql-general@lists.postgresql.org" <pgsql-general@lists.postgresql.org>
Subject: Re: a very primitive question about division

 

Given two values defined as integers, how do I divide one by the other and get an answer with two decimals, e.g 3 /4 = 0.75.

 

​Case one of them to numeric.

 

​select 3/4::numeric

​David J.​

 

Re: a very primitive question about division

От
"David G. Johnston"
Дата:
On Wed, Mar 7, 2018 at 10:30 AM, Martin Mueller <martinmueller@northwestern.edu> wrote:

Thanks.  So  round(before1550/colfreq::numeric, 2)  produces the desired result. 

 

The explanations and examples of string functions in the Postgres documentation are a model of clarity. The explanations and examples of basic arithmetic operations  are not.  There is room for improvement there.



Possibly.  Patches (or even just email suggestions - though the former are generally more apt to get applied) detailing specific improvements to make are welcome.

The description "division (integer division truncates the result)" seems reasonably clear - if you don't want the result truncated, and have two integers, you have to make one of the inputs a non-integer.  Of the various options I tend to choose numeric though others are possible.

David J.

Re: a very primitive question about division

От
Martin Mueller
Дата:

In Table 9.5  the division of 9 by 4 is indeed described clearly. But there is no example of 4/9 and the different ways of formatting it as a decimal fraction with different options for rounding or a percentage.  Two or three added examples would make life easier for folks who have not progressed much beyond 8th grade math.

 

From: "David G. Johnston" <david.g.johnston@gmail.com>
Date: Wednesday, March 7, 2018 at 11:43 AM
To: Martin Mueller <martinmueller@northwestern.edu>
Cc: "pgsql-general@lists.postgresql.org" <pgsql-general@lists.postgresql.org>
Subject: Re: a very primitive question about division

 

Thanks.  So  round(before1550/colfreq::numeric, 2)  produces the desired result. 

 

The explanations and examples of string functions in the Postgres documentation are a model of clarity. The explanations and examples of basic arithmetic operations  are not.  There is room for improvement there.

 

 

Possibly.  Patches (or even just email suggestions - though the former are generally more apt to get applied) detailing specific improvements to make are welcome.

 

The description "division (integer division truncates the result)" seems reasonably clear - if you don't want the result truncated, and have two integers, you have to make one of the inputs a non-integer.  Of the various options I tend to choose numeric though others are possible.

 

David J.

 

Re: a very primitive question about division

От
"David G. Johnston"
Дата:
On Wed, Mar 7, 2018 at 10:56 AM, Martin Mueller <martinmueller@northwestern.edu> wrote:

But there is no example of 4/9 and the different ways of formatting it as a decimal fraction with different options for rounding or a percentage.


​That would be the responsibility of the "Data Type Formatting Functions" chapter and the "to_char" function it describes.

David J.

Re: a very primitive question about division

От
Martin Mueller
Дата:

I see the logic of that, but I also think that this is not how many users think when they want to know how to get results for 4/9.  Note that division is a great stumbling block in middle school.  They want to know how to divide four by nine and don’t want to think about division and formatting as separate operations to be looked up in separate chapters. Perhaps they should think that way, but they don’t, and as Adenauer, modern Germany’s first chancellor,  observed: you have to take people as they come because there are no others.

 

At the least the table 9.5 in 9.3  could have an NB about the problem of decimals in division, together with an example of how to do 4/9 in different formats, and a reference to the fuller discussion.

 

From: "David G. Johnston" <david.g.johnston@gmail.com>
Date: Wednesday, March 7, 2018 at 12:11 PM
To: Martin Mueller <martinmueller@northwestern.edu>
Cc: "pgsql-general@lists.postgresql.org" <pgsql-general@lists.postgresql.org>
Subject: Re: a very primitive question about division

 

But there is no example of 4/9 and the different ways of formatting it as a decimal fraction with different options for rounding or a percentage.

 

​That would be the responsibility of the "Data Type Formatting Functions" chapter and the "to_char" function it describes.

 

David J.