• Re: Formatting a str as a number - Okay, one more related thing...

    From Gilmeh Serda@21:1/5 to Gilmeh Serda on Sat Aug 31 05:31:05 2024
    On Fri, 30 Aug 2024 05:22:17 GMT, Gilmeh Serda wrote:

    f"{int(number):>20,}"

    I can find "," (comma) and I can find "_" (underscore) but how about " " (space)?

    Or any other character, for that matter?

    Any ideas?

    Of course I can do f"{123456:>20_}".replace("_", " "), just thought there
    might be something else my search mojo fails on.

    Thanks.

    --
    Gilmeh

    Diplomacy is to do and say, the nastiest thing in the nicest way. --
    Balfour

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Gilmeh Serda on Sat Aug 31 15:26:58 2024
    Gilmeh Serda <gilmeh.serda@nothing.here.invalid> wrote or quoted:
    Of course I can do f"{123456:>20_}".replace("_", " "), just thought there >might be something else my search mojo fails on.

    Looks like this "replace" deal is the go-to move, no two ways
    about it. If there's some neck of the woods where it's SOP, you
    might wanna roll with "locale.format_string". (Whipping up a custom
    locale just for kicks is usually more trouble than it's worth.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sat Aug 31 17:02:47 2024
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    The German DIN 5008 (their fancy-schmancy rules for writing
    and formatting) now says you got to use /spaces/ to separate
    thousands, and only recommends using periods for cold hard cash.

    And, reportedly, ISO 80000, too.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sat Aug 31 17:00:11 2024
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    Looks like this "replace" deal is the go-to move, no two ways
    about it. If there's some neck of the woods where it's SOP, you
    might wanna roll with "locale.format_string". (Whipping up a custom
    locale just for kicks is usually more trouble than it's worth.)

    The German DIN 5008 (their fancy-schmancy rules for writing
    and formatting) now says you got to use /spaces/ to separate
    thousands, and only recommends using periods for cold hard cash.

    If you just chill for a hot minute, Python's German locale will
    catch up, and you'll be golden.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Gilmeh Serda via Python-list on Sat Aug 31 19:55:39 2024
    On 2024-08-31 06:31, Gilmeh Serda via Python-list wrote:
    On Fri, 30 Aug 2024 05:22:17 GMT, Gilmeh Serda wrote:

    f"{int(number):>20,}"

    I can find "," (comma) and I can find "_" (underscore) but how about " " (space)?

    Or any other character, for that matter?

    Any ideas?

    Of course I can do f"{123456:>20_}".replace("_", " "), just thought there might be something else my search mojo fails on.

    The format is described here:

    https://docs.python.org/3/library/string.html#formatspec

    A space is counted as a fill character.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Stefan Ram on Sat Aug 31 22:56:01 2024
    On 31 Aug 2024 17:00:11 GMT, Stefan Ram wrote:

    The German DIN 5008 (their fancy-schmancy rules for writing and
    formatting) now says you got to use /spaces/ to separate thousands ...

    That was taught to me as the international scientific standard when I was
    in secondary school. Along with using a centred dot for the decimal point,
    to avoid the confusion with region-specific rules involving full stops and commas.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to MRAB via Python-list on Mon Sep 2 12:33:16 2024
    On 1/09/24 06:55, MRAB via Python-list wrote:
    On 2024-08-31 06:31, Gilmeh Serda via Python-list wrote:
    On Fri, 30 Aug 2024 05:22:17 GMT, Gilmeh Serda wrote:

    f"{int(number):>20,}"

    I can find "," (comma) and I can find "_" (underscore) but how about " "
    (space)?

    Or any other character, for that matter?

    Any ideas?

    Of course I can do f"{123456:>20_}".replace("_", " "), just thought there
    might be something else my search mojo fails on.

    The format is described here:

    https://docs.python.org/3/library/string.html#formatspec

    A space is counted as a fill character.


    Rather than strict formatting, you may be venturing into "internationalisation/localisation" thinking.

    Different cultures/languages present numeric-amounts in their own ways.
    For example, a decimal point may look like a dot or period to some
    (there's two words for the same symbol from different English-language cultures!), whereas in Europe the symbol others call a comma is used, eg
    123.45 or 123,45 - and that's just one complication of convention...

    For your reading pleasure, please review "locales" (https://docs.python.org/3/library/locale.html)


    Here's an example:

    Country Integer Float
    USA 123,456 123,456.78
    France 123 456 123 456,78
    Spain 123.456 123.456,78
    Portugal 123456 123456,78
    Poland 123 456 123 456,78


    Here's some old code, filched from somewhere (above web.ref?) and
    updated today to produce the above:-

    """ PythonExperiments:locale_numbers.py
    Demonstrate numeric-presentations in different cultures (locales).
    """

    __author__ = "dn, IT&T Consultant"
    __python__ = "3.12"
    __created__ = "PyCharm, 02 Jan 2021"
    __copyright__ = "Copyright © 2024~"
    __license__ = "GNU General Public License v3.0"

    # PSL
    import locale


    locales_to_compare = [
    ( "USA", "en_US", ),
    ( "France", "fr_FR", ),
    ( "Spain", "es_ES", ),
    ( "Portugal", "pt_PT", ),
    ( "Poland", "pl_PL", ),
    ]

    print( "\n Country Integer Float" )
    for country_name, locale_identifier in locales_to_compare:
    locale.setlocale( locale.LC_ALL, locale_identifier, )
    print( F"{country_name:>10}", end=" ", )
    print(
    locale.format_string("%10d", 123456, grouping=True, ),
    end="",
    )
    print( locale.format_string("%15.2f", 123456.78, grouping=True, ) )

    --
    Regards =dn

    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gilmeh Serda@21:1/5 to Stefan Ram on Tue Sep 3 16:16:46 2024
    On 31 Aug 2024 15:26:58 GMT, Stefan Ram wrote:

    Looks like this "replace" deal is the go-to move,

    Yep, looks like it from the rest of the replies.

    Thanks to all participants.

    --
    Gilmeh

    "You're just the sort of person I imagined marrying, when I was little... except, y'know, not green... and without all the patches of fungus." --
    Swamp Thing

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)