Home > Backend Development > C++ > What does %.21f mean in C language?

What does %.21f mean in C language?

下次还敢
Release: 2024-04-27 23:09:47
Original
1126 people have browsed it

%.21f is the format string of floating point numbers in C language, which means: 21 digits are retained after the decimal point, for example: float num = 123.456789; printf("%.21f", num); // Output: 123.456789012345678901

What does %.21f mean in C language?

The meaning of %.21f in C language

%.21f It is a format string in C language, used to control the output of floating point numbers. It means:

  • %: This is the starting identifier of the format specifier.
  • .: This is a decimal point symbol, indicating that a decimal point should be displayed in the output.
  • 21: This is the number of digits to be displayed after the decimal point.
  • f: This is a floating point numeric character, indicating that a floating point number is to be output.

Thus, %.21f formats a floating point number into a string retaining 21 digits after the decimal point. For example:

<code class="c">float num = 123.456789;
printf("%.21f", num); // 输出:123.456789012345678901</code>
Copy after login

In the above example, the printf() function formats the floating point number num into a string with 21 digits after the decimal point, and which prints to the console.

It’s worth noting:

  • If a floating-point number has fewer digits after the decimal point than the specified number of digits after the decimal point, the output will be padded with zeros.
  • If the floating-point number has more digits after the decimal point than the specified number of digits after the decimal point, it will be rounded or truncated to meet the specified number of digits after the decimal point.
  • If the specified number of decimal places is 0, no decimal point will be displayed in the output.

The above is the detailed content of What does %.21f mean in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template