If you want to format floats with a comma within the f-string, you can either use replace after casting the float to a string:
position = 123.456
f"Position\t{str(position).replace('.',',')}"
A second option is to use the Python standard library module locale (but it is not thread-safe):
import locale
locale.setlocale(locale.LC_ALL, 'nl_NL')
f"Position\t{locale.format('%.3f', position)}"
A third option is to use the library babel (preferred in case of library routines):
from babel.numbers import format_decimal
f"Position\t{format_decimal(position, locale='nl_NL')}"
All three options return the same result for the given example:
'Position\t123,456'
Answer from kadee on Stack OverflowIf you want to format floats with a comma within the f-string, you can either use replace after casting the float to a string:
position = 123.456
f"Position\t{str(position).replace('.',',')}"
A second option is to use the Python standard library module locale (but it is not thread-safe):
import locale
locale.setlocale(locale.LC_ALL, 'nl_NL')
f"Position\t{locale.format('%.3f', position)}"
A third option is to use the library babel (preferred in case of library routines):
from babel.numbers import format_decimal
f"Position\t{format_decimal(position, locale='nl_NL')}"
All three options return the same result for the given example:
'Position\t123,456'
As @michel-de-ruiter mentioned, the f format does not work with locale. On the other hand, you can't set the precision using n format. For example, if you want 4 digits after decimal separator:
import locale
locale.setlocale(locale.LC_ALL, 'nl_NL')
position = 123.45678999
print(f'{position:.4n}') # output: 123,4 (not quite what we wanted!)
However, you can round the number with the desired precision before formatting it:
print(f'{round(position, 4):n}') # output: 123,4567 (that's it!)
python - Fixed digits after decimal with f-strings - Stack Overflow
python - How to print a number using commas as thousands separators - Stack Overflow
f-string - converting float to string with set decimals and thousand separator
How to use python with comma (,) instead of dot (.) as decimal separator?
You convert it before printing.
>>> print(str(3.5).replace(".",","))
3,5Above code converts 3.5 to string, then replaces dot with comma.
More on reddit.comVideos
Include the type specifier in your format expression:
>>> a = 10.1234
>>> f'{a:.2f}'
'10.12'
When it comes to float numbers, you can use format specifiers:
f'{value:<width>.<precision>}'
where:
valueis any expression that evaluates to a numberwidthspecifies the number of characters used in total to display, but ifvalueneeds more space than the width specifies then the additional space is used.precisionindicates the number of characters used after the decimal point
What you are missing is the type specifier for your decimal value. In this link, you an find the available presentation types for floating point and decimal.
Here you have some examples, using the f (Fixed point) presentation type:
# notice that it adds spaces to reach the number of characters specified by width
In [1]: f'{1 + 3 * 1.5:10.3f}'
Out[1]: ' 5.500'
# notice that it uses more characters than the ones specified in width
In [2]: f'{3000 + 3 ** (1 / 2):2.1f}'
Out[2]: '3001.7'
In [3]: f'{1.2345 + 4 ** (1 / 2):9.6f}'
Out[3]: ' 3.234500'
# omitting width but providing precision will use the required characters to display the number with the the specified decimal places
In [4]: f'{1.2345 + 3 * 2:.3f}'
Out[4]: '7.234'
# not specifying the format will display the number with as many digits as Python calculates
In [5]: f'{1.2345 + 3 * 0.5}'
Out[5]: '2.7344999999999997'
Locale-agnostic: use _ as the thousand separator
f'{value:_}' # For Python ≥3.6
Note that this will NOT format in the user's current locale and will always use _ as the thousand separator, so for example:
1234567 ⟶ 1_234_567
English style: use , as the thousand separator
'{:,}'.format(value) # For Python ≥2.7
f'{value:,}' # For Python ≥3.6
Locale-aware
import locale
locale.setlocale(locale.LC_ALL, '') # Use '' for auto, or force e.g. to 'en_US.UTF-8'
'{:n}'.format(value) # For Python ≥2.7
f'{value:n}' # For Python ≥3.6
Reference
Per Format Specification Mini-Language,
The
','option signals the use of a comma for a thousands separator. For a locale aware separator, use the'n'integer presentation type instead.
and:
The
'_'option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type'd'. For integer presentation types'b','o','x', and'X', underscores will be inserted every 4 digits.
I'm surprised that no one has mentioned that you can do this with f-strings in Python 3.6+ as easy as this:
>>> num = 10000000
>>> print(f"{num:,}")
10,000,000
... where the part after the colon is the format specifier. The comma is the separator character you want, so f"{num:_}" uses underscores instead of a comma. Only "," and "_" is possible to use with this method.
This is equivalent of using format(num, ",") for older versions of python 3.
This might look like magic when you see it the first time, but it's not. It's just part of the language, and something that's commonly needed enough to have a shortcut available. To read more about it, have a look at the group subcomponent.