Re: Suppress decimal point like digits in to_char?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
Cc: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Suppress decimal point like digits in to_char?
Date: 2016-03-14 01:53:18
Message-ID: CAKFQuwYuUrkkHLrx6N07SSew=VpN79LRUsJ=v44srSZRCtR9uw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sunday, March 13, 2016, Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com> wrote:

>
> On Mar 13, 2016 6:29 PM, "David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com
> <javascript:_e(%7B%7D,'cvml','david(dot)g(dot)johnston(at)gmail(dot)com');>> wrote:
> >
> > On Sunday, March 13, 2016, Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com
> <javascript:_e(%7B%7D,'cvml','ken(dot)tanzer(at)gmail(dot)com');>> wrote:
> >>
> >> Hi. Is there a way with to_char to suppress a decimal point, like a
> leading or trailing 0, so that integers will not have them, but non-ints
> will? I'm hoping I'm missing something easy. Thanks.
> >>
> >> Ken
> >>
> >> SELECT val,to_char(val::decimal(6,2),'FM999,999D99') FROM
> >> ( SELECT 1 AS val UNION SELECT 1.05 AS val) foo;
> >>
> >> val | to_char
> >> ------+---------
> >> 1 | 1.
> >> 1.05 | 1.05
> >>
> >>
> >
> > Not seeing a native way to do so - and I'd question doing so as a
> general rule - though you know your domain. If you must have this you will
> want to utilize regexp_replace to identify the situation and replace it. A
> simple "\.$" check and a substring would work also.
> >
> > David J.
>
> Thanks David. Just curious what part of this you would question. The case
> for numbers, currency in particular, coming out with a decimal and pennies
> when present, and as whole dollars when not (and without a decimal place at
> the end) seems pretty common and clear cut. What am I missing in your
> question?
>

Typically if I'm going to format any currency amount with pennies I would
format all values, even those with zero pennies, to the same precision.
Typically when displaying such amounts I'd right-justify the values and
thus cause the decimals to line up.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Smith 2016-03-14 02:07:25 Re: retrieve grouping sets/rollup rows
Previous Message James Keener 2016-03-14 01:52:42 Re: retrieve grouping sets/rollup rows