I'm just trying to get my text under "price" to align, and I'm not sure how to do this. All I've done is add spaces in my printf statement to align this little table but obviously,enter image description here when the quantity is more than 1 digit long it offsets to text under price
Asked
Active
Viewed 109 times
-2
-
6Please provide code and output as pre-formatted text, not as images. (And look into the width specifier of `printf` formats.) – M Oehm Oct 04 '19 at 15:16
-
The format, width and precision [specifications](https://learn.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2017) for `printf` are quite involved. It's worth spending some time studying it and experimenting. – Weather Vane Oct 04 '19 at 15:27
-
I tried to help, but my IDE didn't paste your image of code. No code pasted as text == no help. – Thomas Matthews Oct 04 '19 at 16:22
1 Answers
0
printf("Price:\t%10.2f", price);
Here the variable price (a double or float) can be used to insert into %.2f (a format template) which will have 2 decimal places. \t
will be a tab indentation. The + sign aligns it to the right (a - will do it to the left).
Here's a link describing everything!
Thanks to M Oehm

thexiv
- 27
- 9
-
Hm. Depending on the width of the price and the current tabwidth, that might make the output even look worse. It also doesn't line up the decimal points. – M Oehm Oct 04 '19 at 15:28
-
-
The `\t` isn't really the way to do it either. That aligns the left side, not the right. It really needs a format like `"%10.2f"` for example. – Weather Vane Oct 04 '19 at 15:36
-
Hm. The plus sign enforces printig a plus sign before positive values. `%.2` has a precision (how many digits to print), but no width. So remove the tab character and use something like `%10.2f`. – M Oehm Oct 04 '19 at 15:36
-