How to separate float into an integer and a fractional part?

后端 未结 3 960
天涯浪人
天涯浪人 2021-01-04 18:17

I am willing to cast precise operations and for that purpose I need a way to seperate a float number into an integer and a fractional part. Is there any way for this?

3条回答
  •  走了就别回头了
    2021-01-04 19:10

    One other way using type cast.

    #include  
    #include 
    void main()
    { 
        float a = 3.4;
        float a_frac = a - (int) a;
        float a_int = a - a_frac;
        printf("Number = %f, Integer = %f, Fraction = %f", a, a_frac, a_int);
    }
    

提交回复
热议问题