Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-09-04
16:56 • Closed ticket [af4780b0b5]: Improve format #d documentation plus 7 other cha... artifact: f5a972afd2 user: jan.nijtmans
16:19 • Ticket [af4780b0b5]: 4 changes artifact: dde4b7df36 user: oehhar
14:46 • Ticket [af4780b0b5]: 3 changes artifact: 4c5dd20f56 user: jan.nijtmans
14:11 • Ticket [af4780b0b5]: 4 changes artifact: 72bff7c74d user: oehhar
2025-09-03
21:41 • Ticket [af4780b0b5]: 3 changes artifact: f3c0b05a83 user: jan.nijtmans
21:37
[af4780b0b5]: Improve format #d documentation closed check-in: 4abd03dd4c user: jan.nijtmans tags: bug-af4780b0b5
13:50 • Ticket [af4780b0b5] Improve format #d documentation status still Open with 3... artifact: dd0d6dfbeb user: jan.nijtmans
11:38 • Ticket [af4780b0b5]: 3 changes artifact: cbadab6bcf user: oehhar
11:37 • New ticket [af4780b0b5]. artifact: 8b0c235ce2 user: oehhar

Improve format #d documentation

Closed / Fixedaf4780b0b5245f035803b5fbeb1cfae62f40f821Created 1 year agoUpdated 1 year ago
Descriptionwiki2025-09-03 11:37:26
On clt, there was the following proposal by Marc Summerfield on the "#" specifier of the format command man page:
I suggest replacing:

    For d conversions, 0d there is no effect unless the 0 specifier is used
    as well: In that case, 0d will be added to the beginning. 

with:

    For d conversions, 0d has no effect; whereas both #d and #0d produce an
    0d prefix.

Might also be worth adding:

    Note that width includes any prefix. For example "%06d" shows
    at least 6 digits, but "%#06d" shows at least 4 digits.

Evidence:

Main console display active (Tcl9.0.2 / Tk9.0.2)
(mark) 61 % format "%d %0d %06d %#d %#0d %#06d" 12 13 14 15 16 17
12 13 000014 0d15 0d16 0d0017

Remark, that there is a change compared to Tcl 8.6. Tcl 8.6 returned:

format "%d %0d %06d %#d %#0d %#06d" 12 13 14 15 16 17
12 13 000014 15 16 000017
Referenced by1
oehhar Ticket [af4780b0b5]: 3 changes [History diff]2025-09-03 11:38:54
jan.nijtmanswiki2025-09-03 13:50:52

This was specified in TIP #476 as follows:

>Adapt the %#d modifier. The prefix "0d" is only produced when there is a width field and the formatted number is not 0, i.e. when there is possible confusion with an octal number. Since in Tcl 9.0 octal numbers with "0" prefix will be gone, "#" will not produce the decimal prefix any more.

The idea was, for Tcl 8.7, to keep maximum compatibility with 8.6. For Tcl 9.0, the idea was to never output "0d" any more, for maximum compatibility with "C". Apparently, the behavior in Tcl 9.0 was never adapted as described in this TIP; it's still the same as in (never released) 8.7.

Since 9.0 is released now, I think we should keep it as it is now....

jan.nijtmansmarkdown2025-09-03 21:41:03

For completeness:

  • A field with specifier produces a 0d prefix as well

  • If the value is 0, a 0d prefix is never added.

So, we get something like:

For d conversions, there is only effect in combination
with zero-padding or a field width: In that case, 0d
will be added to the beginning of the result unless it is zero.

oehharplain2025-09-04 14:11:57
For me, it would be ok to revert this.
It is really a hack (for 8.7) and breaks 8.6 compatibility for no benefit.

Thanks,
Harald
jan.nijtmanswiki2025-09-04 14:46:13

The benefit is being able to produce formatted integers with "0d", in the same way we can produce hex integers with "0x" prefix.

In TIP #472:

> Also extend format command '#' flag to produce the appropriate "0d" for the "%#d" conversion

I realized that, since the C language doesn't implement the '#' flags for %d, this would be a potential incompatibility. Therefore, in TIP #476 I proposed to only let it have effect in combination with 0-padding or a width specifier: that's the only 2 situations the "0d" prefix really makes sense.

Since Tcl 9.0 is the first Tcl version which understand 0d-prefixed integers, no-one uses this yet. But since the "0d" prefix is advertised as one of the features of Tcl 9, I wounldn't just remove this 'feature'.

oehharwiki2025-09-04 16:19:07

Great! Copied the relevant text from TIP 472 to the migration hints: https://core.tcl-lang.org/tcl/wdiff?id=34d108ee8e05717e.

I don't see, that it only works with zero padding and it is not intended as such. The TIP example is:

% format "%#d" 128
0d128

This works for me.

Thanks for all, Harald

jan.nijtmanswiki2025-09-04 16:56:55

Harald, you are right!

So, documentation updated as well (again), using the same wording as for '0x'

Closing

Log in (anonymous is fine) to add a comment.