• Formatted Integer With Specified Number Of Digits

    From Lawrence D?Oliveiro@3:633/10 to All on Sun Oct 19 07:26:44 2025
    Formatting an integer with 3 digits, excluding base specifier:

    >>> "%#0.3x" % 2
    '0x002'

    No equivalent to this in any of the other ways that Python allows for formatting:

    >>> format(2, "#03x")
    '0x2'

    (Not what I want)

    >>> format(2, "#0.3x")
    Traceback (most recent call last):
    File "<python-input-10>", line 1, in <module>
    format(2, "#0.3x")
    ~~~~~~^^^^^^^^^^^^
    ValueError: Precision not allowed in integer format specifier

    >>> "{:#03x}".format(2)
    '0x2'

    (Not what I want)

    >>> "{:#0.3x}".format(2)
    Traceback (most recent call last):
    File "<python-input-13>", line 1, in <module>
    "{:#0.3x}".format(2)
    ~~~~~~~~~~~~~~~~~^^^
    ValueError: Precision not allowed in integer format specifier

    Why not?

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Gilmeh Serda@3:633/10 to All on Sun Oct 19 09:17:04 2025
    On Sun, 19 Oct 2025 07:26:44 -0000 (UTC), Lawrence D?Oliveiro wrote:

    Why not?

    I have no idea

    you could try zfill:

    f"magic number: {'42'.zfill(4)}"
    'magic number: 0042'

    f"magic number: {str(42).zfill(4)}"
    'magic number: 0042'

    more info:
    https://fstring.help/cheat/

    --
    Gilmeh

    Necessity is a mother.

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From songbird@3:633/10 to All on Sun Oct 19 10:01:16 2025
    Lawrence D?Oliveiro wrote:
    Formatting an integer with 3 digits, excluding base specifier:

    >>> "%#0.3x" % 2
    '0x002'

    No equivalent to this in any of the other ways that Python allows for formatting:

    >>> format(2, "#03x")
    '0x2'

    (Not what I want)

    >>> format(2, "#0.3x")
    Traceback (most recent call last):
    File "<python-input-10>", line 1, in <module>
    format(2, "#0.3x")
    ~~~~~~^^^^^^^^^^^^
    ValueError: Precision not allowed in integer format specifier

    >>> "{:#03x}".format(2)
    '0x2'

    (Not what I want)

    >>> "{:#0.3x}".format(2)
    Traceback (most recent call last):
    File "<python-input-13>", line 1, in <module>
    "{:#0.3x}".format(2)
    ~~~~~~~~~~~~~~~~~^^^
    ValueError: Precision not allowed in integer format specifier

    Why not?


    https://github.com/tpauldike/printf


    songbird

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Sun Oct 19 21:39:17 2025
    On Sun, 19 Oct 2025 10:01:16 -0400, songbird wrote:

    Lawrence D?Oliveiro wrote:

    Formatting an integer with 3 digits, excluding base specifier:

    >>> "%#0.3x" % 2
    '0x002'

    No equivalent to this in any of the other ways that Python allows for
    formatting:

    Why not?


    We already have printf-style formatting in Python, that?s what I was using above.

    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)