From: Erik Wienhold <ewie@ewie.name>
To: magog002@web.de
To: pgsql-general@lists.postgresql.org
Subject: Re: Removing trailing zeros (decimal places) from a numeric (pre trim_scale()) with unexpected behaviour
Date: Wed, 15 Mar 2023 17:50:40 +0100 (CET)
Message-ID: <1053334888.488227.1678899040820@office.mailbox.org> (raw)
In-Reply-To: <trinity-283bd2ce-0cd0-4cf2-a24a-040fa4e5c4e3-1678888297514@3c-app-webde-bs57>
References: <trinity-283bd2ce-0cd0-4cf2-a24a-040fa4e5c4e3-1678888297514@3c-app-webde-bs57>
> On 15/03/2023 14:51 CET magog002@web.de wrote:
>
> I want to remove not needed decimal places / trailing zeros from a numeric.
> I know this can be done starting PG >=13 with TRIM_SCALE(numeric) which would
> solve my issue (with an additional CAST to TEXT at the end). Unfortunately
> the production database is still running with PostgreSQL 12.x and this is
> something I currently can't change.
>
> So to get rid of the not needed decimal places I tried TO_CHAR(..., 'FM....')
> in combination with TRUNC() as shown below with examples. This does not remove
> the decimal places separator if the complete scale digits are zero (60.000).
Cast the to_char result to numeric and then to text. This will also remove
trailing zeros.
select
to_char('60.000'::numeric, 'FM999.999')::numeric::text,
to_char('60.100'::numeric, 'FM999.999')::numeric::text;
to_char | to_char
---------+---------
60 | 60.1
(1 row)
> The current behaviour might be intentional but it 'smells like a bug' to me.
It follows Oracle's to_char behavior:
select to_char('60.000', 'FM999.999') from dual;
TO_CHAR('60.000','FM999.999')
-----------------------------
60.
--
Erik
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: ewie@ewie.name, magog002@web.de
Subject: Re: Removing trailing zeros (decimal places) from a numeric (pre trim_scale()) with unexpected behaviour
In-Reply-To: <1053334888.488227.1678899040820@office.mailbox.org>
References: <trinity-283bd2ce-0cd0-4cf2-a24a-040fa4e5c4e3-1678888297514@3c-app-webde-bs57> <1053334888.488227.1678899040820@office.mailbox.org>
* 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