Re: to_char(): 'FM' also suppresses *trailing* zeroes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Erwin Brandstetter <brsaweda(at)gmail(dot)com>
Cc: pgsql-docs(at)postgresql(dot)org
Subject: Re: to_char(): 'FM' also suppresses *trailing* zeroes
Date: 2017-08-28 20:37:34
Message-ID: 21049.1503952654@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Erwin Brandstetter <brsaweda(at)gmail(dot)com> writes:
> In Table 9-27. "Template Pattern Modifiers for Numeric Formatting" it says:
> FM | prefix fill mode (suppress leading zeroes and padding blanks) | FM9999

> In fact, 'FM' also suppresses *trailing* zeroes after the comma. To fix,
> this might be changed to:
> suppress insignificant zeroes and padding blanks

Not necessarily. A bit of experimentation says that it also matters
whether you use "0" or "9" as the format character:

regression=# select to_char(0.1, '0.9999');
to_char
---------
0.1000
(1 row)

regression=# select to_char(0.1, 'FM0.9999');
to_char
---------
0.1
(1 row)

regression=# select to_char(0.1, '0.9900');
to_char
---------
0.1000
(1 row)

regression=# select to_char(0.1, 'FM0.9900');
to_char
---------
0.1000
(1 row)

regression=# select to_char(0.1, 'FM00.99009');
to_char
---------
00.1000
(1 row)

It's also worth noting the existing examples

regression=# select to_char(-0.1, '99.99');
to_char
---------
-.10
(1 row)

regression=# select to_char(-0.1, 'FM99.99');
to_char
---------
-.1
(1 row)

So it appears to me that the bit you point out is flat out backwards;
what FM actually suppresses is trailing zeroes not leading zeroes.

I'm tempted to propose that in table 9-26, we need to write

9 digit position (can be dropped if insignificant)

0 digit position (cannot be dropped, even if insignificant)

and then in 9-27 say

FM fill mode: suppress trailing zeroes and padding spaces

Also, in between those two tables, I see

* 9 results in a value with the same number of digits as there are
9s. If a digit is not available it outputs a space.

This seems outright wrong per the above examples, and in any case is not
very useful since it doesn't explain the difference from "0". Perhaps
rewrite as

* 0 specifies a digit position that will always be printed,
even if it contains a leading/trailing zero. 9 also specifies
a digit position, but if it is a leading zero then it will be
replaced by a space, while if it is a trailing zero and fill mode
is specified then it will be deleted.

(I wonder how closely that agrees with Oracle's behavior ...)

regards, tom lane

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Tom Lane 2017-08-28 20:53:57 Re: Failing example for to_number()
Previous Message Tom Lane 2017-08-28 20:14:34 Re: Requesting clarifying details on extract(epoch from timestamp)