Reputation: 39
I have been trying to use a variable as a specifer in float precision to try to limit the number of decimal places shown for pi:
num = float(input("What decimal point do you want pi to be displayed to?(Up to 15) "))
print(format(math.pi, '.(num)f'))
when i run it, it shows the ValueError: Format specifier missing precision
I was wondering if there was any other way of writing it so i would be able to use a variable. Im pretty new and its probably a really simple mistake, but i dont know.
Thanks for any help :D
Upvotes: 3
Views: 974
Reputation: 114038
you can just pass it in as a format argument ...
num="3"
print("{pi:0.{num}f}".format(pi=22/7.0,num=num))
Upvotes: 2
Reputation: 106891
You can use the string formatting operator with .*
as the formatting option:
from math import pi
print('%.*f' % (5, pi))
This outputs:
3.14159
Upvotes: 1