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

What does %.2 mean in C language?

下次还敢
下次还敢Original
2024-04-27 23:09:32738browse

%.2f in C language is a format modifier, used to format floating point number output: % indicates the beginning of the format modifier; . indicates the decimal point position; 2 indicates that two decimal places are retained after the decimal point; f Indicates that the data type is a floating point number.

What does %.2 mean in C language?

What does %.2f mean in c language?

%.2f in the C language is a format modifier used to format floating point numbers for output. The

% symbol indicates the beginning of a format modifier. . indicates the position of the decimal point. 2 means keeping two decimal places after the decimal point. f indicates that the data type to be formatted is a floating point number.

Thus, %.2f represents the following format:

  • Retain two decimal places after the decimal point
  • Right-align numbers
  • If the number is negative, add a negative sign at the front

For example:

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

int main() {
    float num = 12.345;
    printf("格式化后的数字:%.2f\n", num);
    return 0;
}</code>

Output:

<code>格式化后的数字:12.35</code>

In this example, The %.2f formatter outputs the floating point number num as a right-justified number with two decimal places after the decimal point.

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

Statement:
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