From: | Francisco Olarte <folarte(at)peoplecall(dot)com> |
---|---|
To: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
Cc: | Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>, PG-General Mailing List <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Suppress decimal point like digits in to_char? |
Date: | 2016-03-14 10:31:47 |
Message-ID: | CA+bJJbz5KBuqs7npX8W1VmetPJfQStGk2yiP27YZ0S_DhRE1Yw@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hi;
On Mon, Mar 14, 2016 at 2:53 AM, David G. Johnston
<david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
> On Sunday, March 13, 2016, Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com> wrote:
....
> 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.
I do format with the .00 too, just wanted to point that the lining up
is easy if you just substitute '.00$' or '\.$' with the correct amount
of space, something like:
s=> select val, tc, '"'||tc||'"' as quoted,
regexp_replace(tc,'\.00$',' ') as replaced from (select val,
to_char(val::decimal(6,2),'999,999D99') as tc from (values
(1),(1.05),(0)) as v(val)) as w;
val | tc | quoted | replaced
------+-------------+---------------+-------------
1 | 1.00 | " 1.00" | 1
1.05 | 1.05 | " 1.05" | 1.05
0 | .00 | " .00" |
(3 rows)
Although I dislike 0 as space, so I normally use '99990':
s=> select val, tc, '"'||tc||'"' as quoted,
'"'||regexp_replace(tc,'\.00$',' ')||'"' as replaced from (select
val, to_char(val::decimal(6,2),'999,990D99') as tc from (values
(1),(1.05),(0)) as v(val)) as w;
val | tc | quoted | replaced
------+-------------+---------------+---------------
1 | 1.00 | " 1.00" | " 1 "
1.05 | 1.05 | " 1.05" | " 1.05"
0 | 0.00 | " 0.00" | " 0 "
(3 rows)
And, if you want to use FM but make them line up on the right is doable too:
s=> select val, tc, '"'||tc||'"' as quoted,
'"'||regexp_replace(tc,'\.$',' ')||'"' as replaced from (select val,
to_char(val::decimal(6,2),'FM999,990D99') as tc from (values
(1),(1.05),(0)) as v(val)) as w;
val | tc | quoted | replaced
------+------+--------+----------
1 | 1. | "1." | "1 "
1.05 | 1.05 | "1.05" | "1.05"
0 | 0. | "0." | "0 "
(3 rows)
But a right-aligning string output routine needs to be used.
cdrs=> select val, tc, '"'||tc||'"' as quoted,
'"'||regexp_replace(tc,'\.$',' ')||'"' as replaced from (select val,
to_char(val::decimal(6,2),'FM999,990D99') as tc from (values
(1234),(1.05),(0)) as v(val)) as w;
val | tc | quoted | replaced
------+--------+----------+------------
1234 | 1,234. | "1,234." | "1,234 "
1.05 | 1.05 | "1.05" | "1.05"
0 | 0. | "0." | "0 "
(3 rows)
Summarising, any combination can be easily done with a single round of replace.
Francisco Olarte.
From | Date | Subject | |
---|---|---|---|
Next Message | Frank Millman | 2016-03-14 14:11:01 | Unexpected result using floor() function |
Previous Message | Michael Paquier | 2016-03-14 08:04:31 | Re: MongoDB 3.2 beating Postgres 9.5.1? |