I have the following:
'{0:n}'.format(0/10.0)
which evaluates to
0
I want it to evaluate to
0.0
That is, there should always be 1 decimal place. What is the correct format for this?
Thanks.
I have the following:
'{0:n}'.format(0/10.0)
which evaluates to
0
I want it to evaluate to
0.0
That is, there should always be 1 decimal place. What is the correct format for this?
Thanks.
print('{0:3.1f}'.format(0/10.0)) # '0.0'
f
for fixed-point notation; 3
for the total number of places to be reserved for the whole string, 1
for the decimal places.
if your number is too big to fit into the 3 reserved spaces, the string will use more space:
print('{0:3.1f}'.format(10.0)) # '10.0'; 4 places
with a more recent python version you can use f-strings (i put the value in a variable for more legibility)
x= 0/10.0
print('f{x:3.1f}')
If you change the data to float you the decimal . a= 1.0 # "a" is a float a= 1 # "a" is now a integer
try that :)