Home >Backend Development >C++ >How to express keeping three decimal places in C language
How to retain three decimal places in C language: use the printf() function; use the format specifier %.3f, where the number followed by . specifies the number of decimal places, and f represents a floating point number.
Representation method with three decimal places in C language
In C language, you can format it by Input/output function printf()
to retain a specified number of decimal places after the decimal point. The specific method is as follows:
Use %.3f
format specifier
%.3f
format specifier is used to specify The format of decimals to print, where:
. The number following
specifies the number of digits after the decimal point. f
represents a floating point number (decimal). Example:
<code class="c">#include <stdio.h> int main() { float number = 123.4567; printf("%.3f", number); // 输出:123.457 return 0; }</code>
Example output:
<code>123.457</code>
Other notes
The above is the detailed content of How to express keeping three decimal places in C language. For more information, please follow other related articles on the PHP Chinese website!