user16613865
user16613865

Reputation: 105

How dynamically change the precision of value using {:10.4f}'.format( [Python]

I have a variable "n" that contains a number of digits that need to be displayed for the value. How to pass the "n" to '{:10.nf}'.format()?

import uncertainties
from uncertainties.umath import *

val_err = uncertainties.ufloat(5, 0.01)
result = val_err*2
n=3  #precision value
result='{:10.nf}'.format(result)
print(result)

Upvotes: 0

Views: 132

Answers (2)

Mattpats
Mattpats

Reputation: 534

result='{0:10.{1}f}'.format(result, n)

Upvotes: 1

bb1
bb1

Reputation: 7873

You can try this:

r = 3.1415926
n = 5

template = f"{{:10.{n}f}}"
result = template.format(r)
print(result)

Upvotes: 1

Related Questions