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

    From Stefan Ram@3:633/280.2 to All on Sun Sep 1 01: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.)



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Sun Sep 1 03: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.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Sun Sep 1 03: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.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From MRAB@3:633/280.2 to All on Sun Sep 1 04: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.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From dn@3:633/280.2 to All on Mon Sep 2 10: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

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: DWM (3:633/280.2@fidonet)