0

Width format conversion

In the following code..the output should be: "New price is 8.50" but the decimal part in the number (8.50) which is (8) should be having 4 digits reserved for it..but it doesn't and I really don't understand it...can anyone please explain this to me ? ------------------------------------ #include <stdio.h> int main() { float price = 6.50; int increase = 2; float new_price; new_price = price + increase; printf("New price is %4.2f", new_price); /* Output: New price is 8.50 */ return 0; }

4th Aug 2020, 5:35 PM
Mohamed Taha
Mohamed Taha - avatar
4 ответов
+ 4
/*Width format conversion In the following code..the output should be: "New price is 8.50" but the decimal part in the number (8.50) which is (8) should be having 4 digits reserved for it..but it doesn't and I really don't understand it...can anyone please explain this to me ? ------------------------------------*/ #include <stdio.h> int main() { float price = 6.50; int increase = 2; float new_price; new_price = price + increase; printf("%06.2f", new_price); // with 0 as fillers /* Output: New price is 8.50 */ printf("\n") ; printf("%6.2f", new_price); // with Space as fillers return 0; }
4th Aug 2020, 5:42 PM
RKK
RKK - avatar
+ 3
Run the above the code and see the difference (The width field is for the entire converted string not just the whole number part).
4th Aug 2020, 5:42 PM
RKK
RKK - avatar
+ 3
4th Aug 2020, 9:28 PM
RKK
RKK - avatar
+ 1
@RKK Run the above the code and see the difference (The width field is for the entire converted string not just the whole number part). ---------------------------------------- So this means the decimal point is also counted in the 6 reserved digits ?
4th Aug 2020, 9:13 PM
Mohamed Taha
Mohamed Taha - avatar