Home >Backend Development >C#.Net Tutorial >How to express keeping two decimal places in C language
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.
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.
After entering the program page, click New File in the upper left corner.
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; }
After writing, click the exclamation mark in the upper right corner , run this program.
Then we can see that 6 decimal places are displayed at the end.
Then change the %lf in the program just now to %.2lf.
#After running this modified program, you can see that 2 decimal places are retained at the end.
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!