In C language, you can obtain the two digits after the decimal point of a floating-point number by following the following steps: Multiply the floating-point number by 100 to turn the decimal part into an integer. Use the / operator to divide a floating point number by 100, rounding off the integer part. Use the % operator to get the decimal part of a floating point number modulo 100.
Take two digits after the decimal point in C language
In C language, you can use the truncated division operation Use the /
operator and the modulo operator %
to obtain the decimal part of a floating point number.
Steps:
/
operators to divide a floating point number by 100 and round the integer part. %
operator to make the floating point number modulo 100 and get the decimal part. Sample code:
<code class="c">#include <stdio.h> int main() { float num = 123.456; int int_part = num / 100; int dec_part = num % 100; printf("小数点后两位数字:%d\n", dec_part); return 0; }</code>
Output:
<code>小数点后两位数字:45</code>
The above is the detailed content of How to express two digits after decimal point in C language. For more information, please follow other related articles on the PHP Chinese website!