pg.ddx.io  pgsql-docs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: barsikdacat@gmail.com
To: pgsql-docs@lists.postgresql.org
Subject: Re: to_char(numeric type, text) rounding instead of truncating
Date: Wed, 26 Jul 2023 07:53:36 +0200
Message-ID: <bab04d8c091629a3f17d476af5eda6982d7e0e0d.camel@cybertec.at> (raw)
In-Reply-To: <168991536429.626.9957835774751337210@wrigleys.postgresql.org>
References: <168991536429.626.9957835774751337210@wrigleys.postgresql.org>

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:

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 5a47ce4343..9421ace77e 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -8505,6 +8505,14 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
       </para>
      </listitem>
 
+     <listitem>
+      <para>
+       If the format provides for fewer fractional digits than the number being
+       formatted has, <function>to_char()</function> will round the number to
+       the specified number of fractional digits.
+      </para>
+     </listitem>
+
      <listitem>
       <para>
        The pattern characters <literal>S</literal>, <literal>L</literal>, <literal>D</literal>,


Yours,
Laurenz Albe





view thread (4+ messages)  latest in thread

Message-ID: <bab04d8c091629a3f17d476af5eda6982d7e0e0d.camel@cybertec.at>
Permalink:  ../bab04d8c091629a3f17d476af5eda6982d7e0e0d.camel@cybertec.at/
Also on:    postgresql.org/message-id/bab04d8c091629a3f17d476af5eda6982d7e0e0d.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-docs@postgresql.org
  Cc: laurenz.albe@cybertec.at, barsikdacat@gmail.com, pgsql-docs@lists.postgresql.org
  Subject: Re: to_char(numeric type, text) rounding instead of truncating
  In-Reply-To: <bab04d8c091629a3f17d476af5eda6982d7e0e0d.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox