When I get the output for print(f"{usernum} divided by {dividenum} is {usernum / dividenum}.")
it will be something like 3.0 rather than 3
How would I remove the .0 from the end.
I'm very new at python.
When I get the output for print(f"{usernum} divided by {dividenum} is {usernum / dividenum}.")
it will be something like 3.0 rather than 3
How would I remove the .0 from the end.
I'm very new at python.
print(f"{usernum} divided by {dividenum} is {usernum / dividenum:.0f}.")
The :.0f
format string syntax is using the fixed-point precision format specifier to round to 0 decimal places. This is functionally the same as using the built-in round function on the resulting float with precision None
.
Other ways of achieving this
# Rounds to nearest whole number
print(f"{usernum / dividenum:.0f}")
print(f"{usernum / dividenum:.0g}")
print(f"{round(usernum / dividenum)}")
print(f"{int(usernum / dividenum)}")
# Convert to string and parse, doesn't round
print(f"{str(usernum / dividenum)[0]}") # Inaccurate after 9
print(f"{str(usernum / dividenum).split('.')[0]}")
The approach needed will depend on your desired output precision.