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

What does %.2f mean in C language?

下次还敢
Release: 2024-04-27 23:06:14
Original
1304 people have browsed it

In C language, %.2f is used to control the output format of floating point numbers, retaining two decimal places after the decimal point. The % in the format specifier indicates the beginning of the format specifier. . separates the integer and decimal parts, and 2 specifies The number of decimal places, f represents a floating point number.

What does %.2f mean in C language?

The meaning of %.2f in C language

In C language, %.2f is a format string used to control the output format of floating point numbers:

  • % means starting a format specifier.
  • . is used to separate the integer part and the decimal part.
  • 2 Specify two decimal places after the decimal point.
  • f represents a floating point number.

Thus, %.2f tells the printf or scanf function to output or input a floating point number in format with two decimal places. Character.

Example:

<code class="c">#include <stdio.h>

int main() {
    float num = 3.141592;
    printf("浮点数为:%.2f\n", num);
    return 0;
}</code>
Copy after login

The above code will output:

<code>浮点数为:3.14</code>
Copy after login

because it formats the floating point number num to two digits Decimal output.

The above is the detailed content of What does %.2f 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