I'm trying to write a function that will change the amount of decimal places depending on the "places" parameter passed into the function. I ended up using if/else statements but this is very limiting.
Is there a way to directly implement the "places" variable into the format method?
I would also like it not to round if possible. And passing in "0" and "3" would return 0.000.
def decimal(num, places):
if places == 1:
print(f'{num:.1f}')
elif places == 2:
print(f'{num:.2f}')
elif places == 3:
print(f'{num:.3f}')
def decimal(num, places):
print(f"{num:.{places}f}")
In my understanding, the above response addresses and resolves your issue.