Home > Backend Development > C++ > body text

How to express two digits after decimal point in C language

下次还敢
Release: 2024-04-27 23:24:28
Original
517 people have browsed it

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.

How to express two digits after decimal point in C language

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:

  1. Multiply the floating point number by 100 so that its decimal part becomes an integer.
  2. Use the / operators to divide a floating point number by 100 and round the integer part.
  3. Use the % 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>
Copy after login

Output:

<code>小数点后两位数字:45</code>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template