+ 3

Can anyone explain what is use of writing "%1.4f" instead of"%f "and same for"%3d "

https://code.sololearn.com/c1Y6o02h6lIl/?ref=app https://code.sololearn.com/cZgprW7CYq3x/?ref=app

10th Dec 2019, 10:34 AM
Pooja
Pooja - avatar
6 Answers
+ 5
Pooja %f means it will print the float value with no gap %2f means it will give gap of 2 spaces then print the value %3.2f In this printf statement we want to print three position before the decimal point (called width) and two positions behind the decimal point (called precision). its just like round the precision value In your code it's used as %1.4f by which float value consist of 1 decimal point width before the decimal and after the decimal it consist of at least 4 digits %3d means add 3 digits width at left side of digit. if in %d digit is of greater then 3 count or 3 then it will be start from no space else <2 digit it will give space at left side another example is posted below https://code.sololearn.com/cyDYZaAh4lDj/?ref=app "Note the format specifier includes 1.4 to indicate the float is to be printed in a space at least 1 characters wide with 4 decimal places."
10th Dec 2019, 10:51 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 4
Pooja %22.4f will give space of 22 before the precision and 4 bits after the precision now if you enter 4.58979 then 4 will print after 21 space and 5897 will print after precision as its reduces to 4 bit
10th Dec 2019, 11:35 AM
MsJ
MsJ - avatar
+ 3
Pooja Let's break it down to simplify stuff % = you are telling system to print a variable f = means print a floating point number d = print an integer 1.4 (in first one) = means you want the system to round off the floating point number to 4 decimal places(see code for given example) and at least use one space to print the number 3(in second one) = means print the veriable with using at least 3 spaces(try printing 1, you will see that the IDE will print "1" after 2 white spaces. Here look at this altered version of your code for more clarification https://code.sololearn.com/cmBXo8jlZ2gq/?ref=app
10th Dec 2019, 10:54 AM
Arsenic
Arsenic - avatar
+ 2
Arsenic ,thank you Can u see my code again, I have made some change in previous one And what I have understood that If I am writing "%22.4f" If we talk about only number before decimal Means compiler allocate total width to a float number is 22 ? am I correct or not?
10th Dec 2019, 11:28 AM
Pooja
Pooja - avatar
+ 1
Pooja, it is simmilar to the %3d, if you write 22.4f then it will use AT LEAST 22 places to display the variable and as you have just filled the veriable with number which occupies only 1 or 2 spaces so just like Mohit the coder (M.S.D) said it will fill the rest of the spaces with......well...spaces. see this code for more info https://code.sololearn.com/chGe0pc3Z7vO/?ref=app
10th Dec 2019, 2:58 PM
Arsenic
Arsenic - avatar
+ 1
Simply for separate numbers when you run ur programme We use this specially in matrix
11th Dec 2019, 10:25 PM
Jong Oum
Jong Oum - avatar