Home >Backend Development >C#.Net Tutorial >How to express keeping two decimal places in C language

How to express keeping two decimal places in C language

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-05-14 16:27:4420936browse

In C language, the representation method is "%.2lf" and the syntax format is "printf("%.2lf,%.2lf\n", element)". "%.2lf" In the "printf()" statement, the entire integer part is output, and the decimal part is output with 2 digits. If it is less than two digits, 0 is added at the end, and if it is more than two digits, it is truncated to two digits.

How to express keeping two decimal places in C language

The operating environment of this tutorial: Windows 7 system, C 17 version, Dell G3 computer.

First click to open the VC 6.0 software on the computer desktop.

How to express keeping two decimal places in C language

After entering the program page, click New File in the upper left corner.

How to express keeping two decimal places in C language

Then write the following program in the newly created file:

#include<stdio.h>

int main(){ 
float a,b; 
a=1.123456; 
b=2.324855; 
printf("%lf,%lf\n",a,b); 
return 0;
}

How to express keeping two decimal places in C language

After writing, click the exclamation mark in the upper right corner , run this program.

How to express keeping two decimal places in C language

Then we can see that 6 decimal places are displayed at the end.

How to express keeping two decimal places in C language

Then change the %lf in the program just now to %.2lf.

How to express keeping two decimal places in C language

#After running this modified program, you can see that 2 decimal places are retained at the end.

How to express keeping two decimal places in C language

Recommended tutorial: "C#"

The above is the detailed content of How to express keeping two decimal places 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