i am given a value e.g. $50.00. The output should be 50.0. Is there a way to make this cleaner? Especially my use of double float is bothering me
float(f"{float(d.removeprefix('$')):.1f}")
CodePudding user response:
Assuming you're working with monetary amounts then this might give you more reliable results:
for d in '$50.99', '$50.00', '49.99':
print(float(f"{float(d.removeprefix('$')):.2f}"[:-1]))
Or...
for d in '$50.99', '$50.00', '49.99':
print(int(float(d.removeprefix('$'))*10)/10)
Output:
50.9
50.0
49.9
CodePudding user response:
You can use round function to limit the result to one decimal.
round(float(d.removeprefix('$')), 1)