C/Console/Formatted Output Float
Содержание
More output format with printf
<source lang="cpp">
- include <stdio.h>
int main(void) {
printf("%.4f\n", 123.1234567); printf("%3.8d\n", 1000); printf("%10.15s\n", "This is a simple test."); return 0;
}
</source>
Output float with format: round
<source lang="cpp">
- include <stdio.h>
void main() {
float radius = 2.0f; /* The radius of a table */ const float Pi = 3.14159f; /* Defines the value of Pi as fixed */ printf("\nThe circumference is %.2f", 2.0f*Pi*radius); printf("\nThe area is %.2f", Pi*radius*radius);
}
</source>
Outputting floating-point values
<source lang="cpp">
- include <stdio.h>
void main() {
float fp1 = 123.678f; float fp2 = 1.2345E6f; double fp3 = 98765432.0; double fp4 = 11.22334455e-6; printf("\n%f %+f % 10.4f %6.4f\n", fp1, fp2, fp1, fp2); printf("\n%e %+E\n", fp1, fp2); printf("\n%f %g %#+f %8.4f %10.4g\n", fp3,fp3, fp3, fp3, fp4);
}
</source>
Printf with format
<source lang="cpp">
- include <stdio.h>
int main(void) {
/* "this is a test" left justified in 20 character field. */ printf("%-20s", "this is a test"); /* a float with 3 decimal places in a 10 character field. The output will be " 12.235". */ printf("%10.3f", 12.234657); return 0;
}
</source>
Use printf to control the output format
<source lang="cpp">
- include <stdio.h>
int main(void) {
double item; item = 10.12304; printf("%f\n", item); printf("%10f\n", item); printf("%012f\n", item); return 0;
}
</source>