Hi;
On Mon, Mar 14, 2016 at 2:53 AM, David G. Johnston
<david.g.johnston@gmail.com> wrote:
> On Sunday, March 13, 2016, Ken Tanzer <ken.tanzer@gmail.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.