0

Why in the first case outputs 3 numbers after ".", And in the second 2 numbers after "."

1;print('{0:.3}'.format(1/3)) , outputs 0.333 2:print('{0:.3}'.format(5/3)) , outputs 1.67

30th Apr 2019, 11:42 AM
Cloudtamer
Cloudtamer - avatar
5 Antworten
+ 2
Scroll up (not down) a bit and read the "None" part (and the "g" part). https://docs.python.org/3.7/library/string.html#format-examples
30th Apr 2019, 1:10 PM
Diego
Diego - avatar
+ 2
if you use additional “f” it will work for both format versions: print('{0:.3f}'.format(1/3)) print('{0:.3f}'.format(5/3)) print(f'{1/3:.3f}') print(f'{5/3:.3f}') # output: 0.333 1.667 0.333 1.667
30th Apr 2019, 1:47 PM
Lothar
Lothar - avatar
+ 1
Not sure, but if you use the format specifier correctly it will work: {0:.3f}
30th Apr 2019, 12:01 PM
Anna
Anna - avatar
0
I’m wondering why it’s exactly 2 digits after the dot, because as I understand it, 0.3 means that the result will be displayed with an accuracy of 3 digits, but in the second case only two digits will be displayed, why?
30th Apr 2019, 12:09 PM
Cloudtamer
Cloudtamer - avatar
0
Thank you all!
30th Apr 2019, 2:16 PM
Cloudtamer
Cloudtamer - avatar