1

I am writing code where I would like the user to enter the desire decimal point precision. For example:

for x in numbers:
    print "{:10.*f}".format(x)

...except where the '*' is I would like to place a variable that which the user provided value. My search for a solution in available documentation has proved unfruitful.

2 Answers 2

1

How about print '{:10.{precision}f}'.format(x, precision=precision), where precision is a value defined elsewhere?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. I was looking for a needle in a haystack. Was nice to have the needle come to me. Thanks again.
No problem. If I helped you, please accept my answer!
1

Python One-liner

number = 10.123456789
print ('{:10.{precision}f}'.format(number, precision=int(input("Enter the precision"))))

Output :   
>>> print ('{:10.{precision}f}'.format(number, precision=int(input("Enter the precision\n"))))
    Enter the precision
    5
      10.12346

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.