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

Liste des GroupesRevenir à cl python 
Sujet : Re: Formatting a str as a number - Okay, one more related thing...
De : PythonList (at) *nospam* DancesWithMice.info (dn)
Groupes : comp.lang.python
Date : 02. Sep 2024, 01:33:16
Autres entêtes
Organisation : DWM
Message-ID : <mailman.26.1725237203.2917.python-list@python.org>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
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

Date Sujet#  Auteur
25 Aug 24 * Re: Formatting a str as a number10Stefan Ram
25 Aug 24 `* Re: Formatting a str as a number9Stefan Ram
27 Aug 24  +- Re: Formatting a str as a number1Stefan Ram
27 Aug 24  `* Re: Formatting a str as a number7Grant Edwards
31 Aug 24   +* Re: Formatting a str as a number - Okay, one more related thing...4Stefan Ram
31 Aug 24   i`* Re: Formatting a str as a number - Okay, one more related thing...3Stefan Ram
31 Aug 24   i +- Re: Formatting a str as a number - Okay, one more related thing...1Stefan Ram
31 Aug 24   i `- Re: Formatting a str as a number (Posting On Python-List Prohibited)1Lawrence D'Oliveiro
31 Aug 24   +- Re: Formatting a str as a number - Okay, one more related thing...1MRAB
2 Sep 24   `- Re: Formatting a str as a number - Okay, one more related thing...1dn

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal