For example, I'm trying to have the following return as 5.90
test = round(5.9, 2)
print(test)
For example, I'm trying to have the following return as 5.90
test = round(5.9, 2)
print(test)
You can't control how many decimals floats have when you print them as is. To have control over that, you need to format the float into a string and then print that.
For example:
(Also: No point in rounding a number with 1 decimal point to the precision of 2 decimal points.)
test = 5.9
print(f"{test:.2f}")
Output:
5.90
You can read more about string formatting here: https://docs.python.org/3/library/string.html#format-specification-mini-language
You can use %.2f instead.. for example:
test = round(5.9, 2)
print("%.2f"%test)
it will output 5.90.u can replace 2 with how many u wan to have numbers after decimal point
this works for python2 and python3
n = 5.9
print('%.2f' % n) # -> 5.90
in python3 also works
print('{:.2f}'.format(n)) # -> 5.90
and since python 3.7 also works:
print(f'{n:.2f}') # -> 5.90