Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trouble with using variables as format precision

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

like image 736
Ryan pietras Avatar asked Jan 27 '26 10:01

Ryan pietras


1 Answers

you can just pass it in as a format argument ...

num="3"
print("{pi:0.{num}f}".format(pi=22/7.0,num=num))
like image 121
Joran Beasley Avatar answered Jan 29 '26 00:01

Joran Beasley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!