How to extract the decimal part from a floating point number in C?

后端 未结 14 1994
旧巷少年郎
旧巷少年郎 2020-11-27 04:04

How can we extract the decimal part of a floating point number and store the decimal part and the integer part into two separate integer variables?

14条回答
  •  时光取名叫无心
    2020-11-27 04:40

    You use the modf function:

    double integral;
    double fractional = modf(some_double, &integral);
    

    You can also cast it to an integer, but be warned you may overflow the integer. The result is not predictable then.

提交回复
热议问题