How can I print only the fractional portion of the number

5

Good evening, I would like to know how I can print the fractional part of the real number, that is, when I type 5.678, I would like to print in the second A (where the comment is), just the number 0.678, which I should use for the impression, I did not find in math.h any that solved the problem. Thankful.

#include <stdio.h>

int main (void)

{
    float A;
    scanf("%f", &A);

    A = ceil(A);
    A = ;//Impressão nesta linha da parte fracionaria
    A = floor(A);

    printf("%f\n%.0f\n%f", A, A, A);


return 0;   
}
    
asked by anonymous 19.04.2015 / 01:05

2 answers

7

A simple suggestion is to do a type casting of the number for an integer, and then subtract that integer value from the original number. For example:

printf("%f", A - ((int) A));

So, if the value of A is 5.678 , the resulting operation will be:

5.678 - ((int) 5.678) = 5.678 - 5 = 0.678
    
19.04.2015 / 01:25
-1

You're changing the value of A in your code

    // digamos que o utilizador digitou 5.678
    A = ceil(A);                    // A passou a 6
    A = ;          // a parte fracionaria de 6 'e 0
    A = floor(A);                   // A passou a 6

I suggest you get other variables

    // digamos que o utilizador digitou 5.678
    B = ceil(A);                    // B passou a 6
    C = ;                           // ??
    D = floor(A);                   // D passou a 5
    
19.04.2015 / 11:02