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.
<code>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}')
</code>
<code>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}')
</code>
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}')
New contributor
Erin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.