Hi everyone,
How show decimal only for none whole number ?
and when the number have decimal limit their number to two.
I've tried this :
test = 65654848
print(f'{test:,.2f})
Output:
65,654,848.00
not what i'm looking for

I don't understand the question. What output do you want?
Thanks @
ndc85430
Actually I've found a solution, not the most elegant but it work
def F_minimalNumber(x):
f = float(x)
if f.is_integer():
return (f'{int(f):,}'.replace('.','_').replace(',','.').replace('_',','))
else:
return (f'{round(f,2):,}'.replace('.','_').replace(',','.').replace('_',','))
print(F_minimalNumber(65654848.6546843))
print(F_minimalNumber(65654848))
So it's round the number when have decimal and show maximum 2 decimal.
and show no decimal ex:(.00) when no decimal
and change the , for . for the thousand separator.
Output:
65.654.848,65
65.654.848
Simple way
#! /usr/bin/python3.8
num = 123
num2 = 123.5012569
num3 = 123.05
def numbers(num):
if isinstance(num, float):
print('%.2f' % round(num, 2))
else:
print(num)
numbers(num)
numbers(num2)
numbers(num3)
Output:
123
123.50
123.05
Just wanted to add one
#! /usr/bin/python3.8
num = 123
num2 = 123.5012569
num3 = 123.05
num4 = 125.00
def numbers(num):
if num == int(num):
print(int(num))
elif isinstance(num, float):
print('%.2f' % round(num, 2))
else:
print(num)
numbers(num)
numbers(num2)
numbers(num3)
numbers(num4)
Output:
123
123.50
123.05
125
This is only for sake of humans to improve readability. Python can't perform calculations with values formatted this way (except string concatenation).
import locale
lang, encoding = locale.getlocale()
locale.setlocale(locale.LC_ALL, lang)
x = 1337.42
print(f"{x:n}")
What is n?
This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters.
https://docs.python.org/3/library/string...matstrings