Print a large integer using sanctions using the Python3 mini-language

I want a dot after every three digits of a large number (e.g. 4.100.200.300 ).

 >>> x = 4100200300 >>> print('{}'.format(x)) 4100200300 

This question relates to the Pythons string formatting mini-language.

+5
source share
1 answer

There is only one thousands separator available.

The ',' option signals the use of a comma for the thousands separator.

( docs )

Example:

 '{:,}'.format(x) # 4,100,200,300 

If you need to use a period as a thousands separator, consider replacing commas with '.' or set the locale (category LC_NUMERIC) accordingly.

You can use this list to find the correct language. Note that you will need to use the n integer view type for local formatting:

 import locale locale.setlocale(locale.LC_NUMERIC, 'de_DE') # or da_DK, or lt_LT, or mn_MN, or ... '{:n}'.format(x) # 4.100.200.300 

In my opinion, the previous approach is much simpler:

 '{:,}'.format(x).replace(',', '.') # 4.100.200.300 

or

 format(x, ',').replace(',', '.') # 4.100.200.300 
+6
source

Source: https://habr.com/ru/post/1237044/


All Articles