• Difference in return value of format in Tcl 9 and 8

    From meshparts@[email protected] to comp.lang.tcl on Wed Sep 3 10:23:33 2025
    From Newsgroup: comp.lang.tcl

    I have this string formatting command:

    format "%#9i" 123

    With Tcl 8 I get " 123"

    With Tcl 9 I get " 0d123"

    The usage of the "#" flag comes from a more general procedure, that
    makes sure, that in case of "e" format (scientific notation) there is
    always a decimal point available.

    In Tcl 8 "#" had no effect on "i" format.

    In Tcl 9 it add "0d" to the result.

    Is this a bug or intention?
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Harald Oehlmann@[email protected] to comp.lang.tcl on Wed Sep 3 10:32:33 2025
    From Newsgroup: comp.lang.tcl

    Am 03.09.2025 um 10:23 schrieb meshparts:
    I have this string formatting command:

    format "%#9i" 123

    With Tcl 8 I get "      123"

    With Tcl 9 I get "    0d123"

    The usage of the "#" flag comes from a more general procedure, that
    makes sure, that in case of "e" format (scientific notation) there is
    always a decimal point available.

    In Tcl 8 "#" had no effect on "i" format.

    In Tcl 9 it add "0d" to the result.

    Is this a bug or intention?

    I have no idea.
    Perhaps open a ticket on tcl?
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ralf Fassel@[email protected] to comp.lang.tcl on Wed Sep 3 12:41:35 2025
    From Newsgroup: comp.lang.tcl

    * meshparts <[email protected]>
    | I have this string formatting command:

    | format "%#9i" 123

    | With Tcl 8 I get " 123"

    | With Tcl 9 I get " 0d123"

    | The usage of the "#" flag comes from a more general procedure, that
    | makes sure, that in case of "e" format (scientific notation) there is
    | always a decimal point available.

    | In Tcl 8 "#" had no effect on "i" format.

    | In Tcl 9 it add "0d" to the result.

    | Is this a bug or intention?

    https://www.tcl-lang.org/man/tcl9.1/TclCmd/format.html#M12

    #
    Requests an alternate output form. For o conversions, 0o will be
    added to the beginning of the result unless it is zero. For x or X
    conversions, 0x will be added to the beginning of the result unless
    it is zero. For b conversions, 0b will be added to the beginning of
    the result unless it is zero.
    !! 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.
    [...]

    The sentence for the 'd' conversion does not really make sense 100% to me (non-english speaker), but it sounds as if "%#9i" should *not* produce
    the 0d, since the '0 specifier' is not present.

    HTH
    R'
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From meshparts@[email protected] to comp.lang.tcl on Wed Sep 3 12:51:29 2025
    From Newsgroup: comp.lang.tcl

    Am 03.09.2025 um 12:41 schrieb Ralf Fassel:
    * meshparts <[email protected]>
    | I have this string formatting command:

    | format "%#9i" 123

    | With Tcl 8 I get " 123"

    | With Tcl 9 I get " 0d123"

    | The usage of the "#" flag comes from a more general procedure, that
    | makes sure, that in case of "e" format (scientific notation) there is
    | always a decimal point available.

    | In Tcl 8 "#" had no effect on "i" format.

    | In Tcl 9 it add "0d" to the result.

    | Is this a bug or intention?

    https://www.tcl-lang.org/man/tcl9.1/TclCmd/format.html#M12

    #
    Requests an alternate output form. For o conversions, 0o will be
    added to the beginning of the result unless it is zero. For x or X
    conversions, 0x will be added to the beginning of the result unless
    it is zero. For b conversions, 0b will be added to the beginning of
    the result unless it is zero.
    !! 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.
    [...]

    The sentence for the 'd' conversion does not really make sense 100% to me (non-english speaker), but it sounds as if "%#9i" should *not* produce
    the 0d, since the '0 specifier' is not present.

    HTH
    R'
    Thanks.
    I sent a TIP to [email protected]
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@[email protected] to comp.lang.tcl on Wed Sep 3 11:11:32 2025
    From Newsgroup: comp.lang.tcl

    On Wed, 03 Sep 2025 12:41:35 +0200, Ralf Fassel wrote:

    * meshparts <[email protected]>
    | I have this string formatting command:

    | format "%#9i" 123

    | With Tcl 8 I get " 123"

    | With Tcl 9 I get " 0d123"

    | The usage of the "#" flag comes from a more general procedure, that
    | makes sure, that in case of "e" format (scientific notation) there is
    | always a decimal point available.

    | In Tcl 8 "#" had no effect on "i" format.

    | In Tcl 9 it add "0d" to the result.

    | Is this a bug or intention?

    https://www.tcl-lang.org/man/tcl9.1/TclCmd/format.html#M12

    #
    Requests an alternate output form. For o conversions, 0o will be
    added to the beginning of the result unless it is zero. For x or X
    conversions, 0x will be added to the beginning of the result unless
    it is zero. For b conversions, 0b will be added to the beginning of
    the result unless it is zero.
    !! 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.
    [...]

    The sentence for the 'd' conversion does not really make sense 100% to me (non-english speaker), but it sounds as if "%#9i" should *not* produce
    the 0d, since the '0 specifier' is not present.

    HTH
    R'

    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

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Harald Oehlmann@[email protected] to comp.lang.tcl on Wed Sep 3 13:40:49 2025
    From Newsgroup: comp.lang.tcl

    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


    Thanks, Marc.
    I added this information to a ticket: https://core.tcl-lang.org/tcl/info/af4780b0b5245f03

    It might be good to add this to the migration hints, as the Tcl 8.6
    result is quite different:

    % format "%d %0d %06d %#d %#0d %#06d" 12 13 14 15 16 17
    12 13 000014 15 16 000017

    Do you have the rationale for this change? It looks like intention and
    not a bugfix.

    Thanks for all,
    Harald
    --- Synchronet 3.21a-Linux NewsLink 1.2