How can I print only the fractional part of the number

Asked

Viewed 2,880 times

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 this comment), only the number 0.678, what I should use for printing, I did not find in Math. there is one that solves the problem. Thank you.

#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;   
}
  • 1

    Note, however, that your printf end will not give the result you expect. You use the same variable to store different values, but its output will have the same value repeated three times (which will be the value of the last assignment to A, that is, the value of floor(A)). Use different variables to receive and store the results of operations on A.

2 answers

7

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

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

Thus, if the value of A for 5.678, the resulting operation will be:

5.678 - ((int) 5.678) = 5.678 - 5 = 0.678
  • Luiz, it would be interesting before what I raised in the goal (http://meta.pt.stackoverflow.com/a/4083/8039) to add the @pmg response to yours, and also correct the code so that the answer is more complete, what do you think?

  • Delfino, myself I have already commented on the additional problem in the AP code. But since this problem has nothing to do with the question, I don’t see why I should edit my answer to that (hardly that part would help someone else). The answer of the colleague @pmg is useful to the AP, but in my opinion he (pmg) would do well to complement as suggested by the bfavaretto there at the Meta (because then would no longer deserve your -1) :)

-1

You change 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 find other variables

    // digamos que o utilizador digitou 5.678
    B = ceil(A);                    // B passou a 6
    C = ;                           // ??
    D = floor(A);                   // D passou a 5
  • 1

    Doesn’t answer the question, should be as a comment.

  • Perfectly agreed, @Delfino ... but it didn’t seem to me that the format of the comments made this note understandable.

  • agree @pmg, I will take this question to the goal, because I really do not know which is the best, but I understand that is not an answer. :)

  • 1

    open at the goal in order to mature such a question and who knows how to suggest improvements in the comment mechanism, or who knows how to define as good practice directly edit the question and correct the code: http://meta.pt.stackoverflow.com/q/4082/8039

Browser other questions tagged

You are not signed in. Login or sign up in order to post.