I'd like to print a float number with only 2 decimal places and align it to the right in a 6 characters space.
I tried to do this, but didn't work:
printf("%6.2f", value);
What you've posted will fit the whole float into a 6 char wide column with the .xx taking up the last 3. If you want the integer portion in a six char wide column with the '.' and the fractional portion after these 6 characters, its %9.2f. Quick example program to show the differences
#include <stdio.h>
int main(void) {
float x = 83.4;
printf("....|....|....|\n");
printf("%6.2f\n", x); // prints " 83.40"
printf("%9.2f\n", x); // prints " 83.40"
return 0;
}
And the output:
....|....|....|
83.40
83.40
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With