Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to control the number of significant digits in output numbers in C language
How to control the number of significant digits in output numbers in C language

Just add the printf function at the end of the output:

Double type data: printf("%.5lf\n",a);

< p>float type data: printf("%.5f\n",a);

where printf("%n.mlf\n",a); n is the number of digits in one, m is the number of decimal places.

The following is the format:

printf ("format control string", output table column)

The format control string is %- 0 m.n l/h ?Format character, % is the leading symbol of the format description, - specifies left-facing output, 0 fills in the specified gap with 0, m.n specifies the output domain width and precision, l/h is the correction of the output length, the format character is the specified output data type.

Extended information:

In addition to the effective number of output numbers that need to be controlled, the accuracy also needs to be controlled.

The precision format character starts with "." followed by a decimal integer. Possible values ??are as follows:

Decimal integer.

(1) For integers (d, i, o, u, x,

(2) For floating point types (a, A, e, E, f), precision represents the number of digits after the decimal point. The default is six digits. If any number is missing, it will be set to 0. If it exceeds, it will be truncated.

(3) For type specifier g or G, it represents the maximum significant number that can be output.

(4) For strings (s), precision indicates the maximum number of characters that can be output. If it is less than the normal output, it will be truncated if it exceeds it.

If precision is not explicitly specified, it defaults to 0

Use asterisks instead of numerical values, similar to * in width, to specify precision in the output parameter list.