0

.format() problem

Im testing the .format() function, I came across something I don't understand a = 1.23456 print("{a:0.3f}".format(a=a)) output -> 1.235 why is that? {3f} is supposed to give 3 digits of numbers at the back right?? if i put {4f} the output is -> 1.2346 why?

13th Jul 2020, 4:41 AM
slawbear
slawbear - avatar
3 Antworten
+ 3
The number to the left of the '.' is the minimum width (default is left space padded) The number to the right of the '.' is the number of digits after the decimal point rounded. The f indicates it is a float point value 0.3f is a float value with a 0 min width rounded to the 3rd decimal place '1.235' 4f is a float value with a min width of 4 It outputs '1.234560' The width of the output is greater than the specified width so it goes unnoticed. If you had 10.2f then the output would have a total width of 10 spaces and be rounded to the second decimal place. ' 1.23' Where '1.23' only takes up 4 spaces of the width so the output is left padded with 6 spaces. Some more info on formatting strings: https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-formatted-output/
13th Jul 2020, 5:23 AM
ChaoticDawg
ChaoticDawg - avatar
0
It is rounding off ...if you input 1.2385 ..output will be 1.239
13th Jul 2020, 5:34 AM
Suhail KM
0
.format fill the {} in the string in python.
14th Jul 2020, 1:57 PM
shubham kumar
shubham kumar - avatar