Re: to_char(numeric type, text) rounding instead of truncating

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: barsikdacat(at)gmail(dot)com, pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: to_char(numeric type, text) rounding instead of truncating
Date: 2023-09-06 20:53:15
Message-ID: ZPjmu9EEMOcfkVsv@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On Wed, Jul 26, 2023 at 07:53:36AM +0200, Laurenz Albe wrote:
> On Fri, 2023-07-21 at 04:56 +0000, PG Doc comments form wrote:
> > The following documentation comment has been logged on the website:
> >
> > Page: https://www.postgresql.org/docs/15/functions-formatting.html
> > Description:
> >
> > I've encountered an issue with to_char(numeric type, text)
> > Apparently, it uses rounding instead of truncation which leads to
> > unpexpected results. This is not reflected in the documentation.
> > My specific example:
> > select to_Char(1235::real/(2::real+1235::real)*100, '99%')
> > The expected output was 99%. The output was ##% - since the number was
> > rounded to 100, there wasn't enough digits in the format.
> > If I add a digit after the decimal point
> > select to_char(1235::real/(2::real+1235::real)*100,'90D0%')
> > The output becomes 99.8%
> >
> > It would be nice to have this behavior explained in the documentation so
> > people get more predictable results.
>
> +1
>
> How about the following:

I slightly modified your patch and applied it back to PG 11 since all
supported Postgres versions have the same behavior. Thanks.

--
Bruce Momjian <bruce(at)momjian(dot)us> https://momjian.us
EDB https://enterprisedb.com

Only you can decide what is important to you.

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Laurenz Albe 2023-09-06 20:58:50 Re: to_char(numeric type, text) rounding instead of truncating
Previous Message Bruce Momjian 2023-09-06 14:34:24 Re: Replica vs standby