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

    From Gilmeh Serda@gilmeh.serda@nothing.here.invalid to comp.lang.python on Sat Aug 31 05:31:05 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Sat Aug 31 15:26:58 2024
    From Newsgroup: comp.lang.python

    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.)


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Sat Aug 31 17:00:11 2024
    From Newsgroup: comp.lang.python

    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.


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Sat Aug 31 17:02:47 2024
    From Newsgroup: comp.lang.python

    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.


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From MRAB@python@mrabarnett.plus.com to comp.lang.python on Sat Aug 31 19:55:39 2024
    From Newsgroup: comp.lang.python

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From dn@PythonList@DancesWithMice.info to comp.lang.python on Mon Sep 2 12:33:16 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Gilmeh Serda@gilmeh.serda@nothing.here.invalid to comp.lang.python on Tue Sep 3 16:16:46 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114