Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qdzWe-005Pj8-5F for pgsql-docs@arkaria.postgresql.org; Wed, 06 Sep 2023 20:53:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qdzWc-00DCig-TG for pgsql-docs@arkaria.postgresql.org; Wed, 06 Sep 2023 20:53:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qdzWc-00DCiL-L9 for pgsql-docs@lists.postgresql.org; Wed, 06 Sep 2023 20:53:22 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qdzWY-003UI8-Jy for pgsql-docs@lists.postgresql.org; Wed, 06 Sep 2023 20:53:21 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2023062407; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description; bh=FCfDCqtefqN4hUsVwjaxx5YbFSsB5eGNSy4PibFDThQ=; b=osyID hG35r4vYUFHphJsHCoX09wfNTxjqu7Mwohp+Fwx+bTjKayPVTDwLbr4kJcHM67c7D08lmY45skUMr +qr/henx/m9uodde+gaUtHXt34TGRZuaWWJhiwIQ0uJPJmV5PuLe48LfwO+dYBt1z9fK+Ew8nRHVZ sG6wCs8RGXDCo2NdhbNYkBMzDrh6h5t8THTvNxfJYFdcGwt7TkVSbXH+mfEJqWGv0akudeHLnFceu Hq2AX8ZF41gJ2O8CEGwhnNVZWlkpnBpxMATKf0HDAaZSUtHXLCX3S22MCphs5hNf2ef0Ed4LD7iP9 iqa4t8Lo0zkLZ8Wb4OdNhuW6qweKQ==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1qdzWV-0051zY-2E; Wed, 06 Sep 2023 16:53:15 -0400 Date: Wed, 6 Sep 2023 16:53:15 -0400 From: Bruce Momjian To: Laurenz Albe Cc: barsikdacat@gmail.com, pgsql-docs@lists.postgresql.org Subject: Re: to_char(numeric type, text) rounding instead of truncating Message-ID: References: <168991536429.626.9957835774751337210@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you.