0
What is the different when I use %5.2f and %1.2f
Let say x=3.14159 and I want to print x out And I use %5.2f it give me 3.14 and the same with %1.2f Then what the different here?
1 Answer
+ 4
This is the way it works.
Suppose it's like %N.Kf, understand it like, it'll reserve a total of N spaces(including the decimal) in which K spaces will be reserved for precision or for the digits after the decimal point.
So, here if your number is 3.14159 and you're using %5.2f, it'll reserve a total of 5 spaces in which 2 are for precision digits but since there's just one digit before the decimal part so it'll print like, 03.14(5 places with 2 precision places).
But when you format like %1.2f, you're actually saying it to reserve a total of one place(obv. including the decimal point) with having 2 precision digits, which is, obviously, absurd so it'll override your format with having 2 digits after decimal i.e., in this case a total of 4 places and that's why the output for the two are same.
Try playing with it using different format and numbers to get most out of it.