Re: Suppress decimal point like digits in to_char?

Поиск
Список
Период
Сортировка
От Francisco Olarte
Тема Re: Suppress decimal point like digits in to_char?
Дата
Msg-id CA+bJJbz5KBuqs7npX8W1VmetPJfQStGk2yiP27YZ0S_DhRE1Yw@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Suppress decimal point like digits in to_char?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Ответы Re: Suppress decimal point like digits in to_char?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general
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.


В списке pgsql-general по дате отправления:

Предыдущее
От: Michael Paquier
Дата:
Сообщение: Re: MongoDB 3.2 beating Postgres 9.5.1?
Следующее
От: "Frank Millman"
Дата:
Сообщение: Unexpected result using floor() function