So I need to do some division. Does anyone know how to output the result with three decimal places? For example: –>
5/2 = 2.500 8/6 = 1.333 10/3 = 3.333
Tried code:
a = int(input())
b = int(input())
print(a/b)
You can see, if the input is 5 and 2 it will output 2.5(not 3 decimal places)
>Solution :
print(f'{a/b:.3f}')
Manual on formatting mini-language : https://docs.python.org/3/library/string.html#formatspec
This will print 2/3 as 0.666, if you want 0.667 use :.3g instead.
f= fixed pointg= general format