Home > Back-end >  C how to retain the decimal output
C how to retain the decimal output

Time:03-05

The header file: # include & lt; Iomanip>
Content: cout< fixed (keep digits) & lt;
Fixed flow (operator) meaning: it means the floating-point output should be displayed in a fixed point or decimal notation
If only setprecision () function, so he is effective control of number of digits, including the front of the decimal point!!!!
So generally keep fixed and setprecision even to achieve the decimal,

CodePudding user response:

pure C language was strictly distinguish between how many decimal places and how many of these two concepts, effective digital

The Format Specification Fields: printf and wprintf Functions provides
A format specification, which consists of optional and required fields, has the following form:

% [flags] [width] [. Precision] [{h | l | I64 | l}] type

The Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent signs and type a character (for example, % s). If a percent sign is followed by a character that has no fancy as a format field, the character is copied to stdout. For example, to print a percent - sign character, use % %.

The optional fields, which appear before The type, character, control other aspects of The formatting, as follows:

Type

The Required character that determines been the associated argument is interpreted as a character, a string, or a number (see Table r.. 3).

Flags

Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table r. 4) wining one flag can appear in a format specification.

Width

Optional number that specifies the minimum number of characters. The output (See printf Width Specification.)

Precision

Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of who printed for integer values (see Table r. 5).

H | l | I64 | l

Optional prefixes to type - that specify the size of argument (see Table r. 6).


Precision Specification
The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table R.5). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:

Printf (" % 0 d ", 0)./* No characters output */

If the precision specification is an asterisk (*), an int argument from the argument list supplies the value, the precision argument must precede the value being formatted in the argument list.

The type determines The interpretation of precision and The default when precision is omitted, to The Table in r. 5.

5 How Precision Table r. Values Affect Type

Type a fancy Default
C, c The precision has no effect. The Character is printed.
D, I, o, u, x, x The precision specifies The minimum number of who to be printed. If The number of who in The argument is less than The precision, The output value is padded on The left with zeros. The value is not truncated when The number of who exceeds precision. The Default precision is 1.
E, e, The precision specifies The number of who to be printed after The decimal point. The last printed digit is rounded. The Default precision is 6; If precision is zero or the period (.) appears without a number following it, no decimal point is printed.
f The precision value specifies The number of who after The decimal point. If a decimal point appears, The at further one digit appears before it. The value is rounded to The appropriate number of who. The Default precision is 6; If precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.
G, g The precision specifies The maximum number of significant who printed. Six significant who are printed, with any trailing zeros truncated.

S, s The precision specifies The maximum number of characters to be printed. The characters in excess of precision are not printed. The characters are printed until a null character is encountered.


If the argument corresponding to a floating - point specifiers is infinite, indefinite, or NaN, printf gives the following output.

The Value Output
+ infinity 1 # INFrandom - who
- infinity - 1 # INFrandom - who
Indefinite (same as the -quiet NaN) digit. # INDrandom - who
NAN digit. # NANrandom - who

CodePudding user response:

Ok thank you for your counsel
  • Related