Search code examples
cmathfloating-pointinteger

How to detect if a number is a whole number or a float in C?


So I am new to C, and I mainly use it to make calculators for probability and stuff like that. My current project finds the increase and decrease of 2 values.

The code is:

#include <stdio.h>
//Variables

//Main Area
int main()
{
    float num1;
    float num2;
    float num3;
    float num4;
    float answer;
    //Scanning for the first number
    printf("\n Please Enter the first Value : ");
    scanf("%f", &num1);
    //scanning for the second number
    printf("\n Please Enter the second Value : ");
    scanf("%f", &num2);
    //calculating
    num3 = num1 - num2;
    num4 = num3 / num1;
    answer = num4 * 100;
    //Checking if the answer is an increase or decrease
    if (answer > 0) {
        printf("The answer has been decreasedby: %f\n", answer);
        printf("percent");
    } else {
        printf("The answer has been increased by: %f\n", answer * -1);
        printf("percent");
    }
  
    //Printing the answer
 
}

The output:

 Please Enter the first Value: 9                                                                                                                            
 Please Enter the second Value : 90                                                                                            
The answer has been increased and the final value is: 900.000000                                                               
percent

So I set all the values as floats rather than int because int only support whole numbers. But when I do get a whole number rather then displaying only the single number with no decimal points, it produces a number with the number and a bunch of zeros after the decimal point. Is there a way to detect if the number Is a whole number and just display that number?


Solution

  • Compare answer to the return value of f.e. the floorf() function (header math.h) - floorf() since you dealing with a variable of type float - with answer passed as argument.

    By doing so, you proof if answer contains an even decimal value.

    For the output itself, Use a precision specifier (f.e. .0) which specifies the amount of digits in the fraction part and place it between the % and the f conversion specifier, For example %.0f.

    The precision of .0 specifies that you want to display no fraction part.

    // Checking if the answer is an increase or decrease
    
    if ( answer > 0 ) {
    
          if ( answer == floorf(answer) )     // If answer contains a decimal value.
              printf("The answer has been decreased and the final value is: %.0f\n", answer);
          else
              printf("The answer has been decreased and the final value is: %.3f\n", answer);
    }
    else {
    
          if ( answer == floorf(answer) )     // If answer contains a decimal value.
              printf("The answer has been increased and the final value is: %.0f\n", answer * -1);
          else 
              printf("The answer has been increased and the final value is: %.3f\n", answer * -1);     
    }
    

    Side Notes:

    1. Note that floating-point often does not provide expected results: Is floating point math broken? So, it is possible that even this technique will not work at each and every use case.

    2. Always check the return value of input functions such as scanf() if an input error occurred.

    3. I used floorf(), but ceilf(), truncf() or roundf() could also be used to achieve the same effect.

    4. int main() is deprecated. Use int main(void) instead which declares a full prototype for main().

    5. Since answer will be negative when reaching the else body, printing answer * -1 will output a positive value, since - * - = +. Maybe this is not intended, so this just as a hint.