如何检测一个数字是整数还是C中的浮点数?

com*_*ery 1 c math floating-point integer

所以我是 C 的新手,我主要用它来制作概率计算器和类似的东西。我当前的项目发现了 2 个值的增加和减少。

代码是:


#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
  
 
}


Run Code Online (Sandbox Code Playgroud)

输出:

 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
Run Code Online (Sandbox Code Playgroud)

所以我将所有值设置为Floats而不是Ints因为Ints只支持整数。但是当我得到一个整数而不是只显示没有小数点的单个数字时,它会产生一个带有数字和小数点后一堆零的数字。有没有办法检测数字是否是整数并只显示该数字?

谢谢

chu*_*ica 7

有没有一种方法可以检测答案是否为整数并只显示该数字?

要检测是否float为整数,请使用modff()将 afloat分解为整数和小数部分。

float f;
float ipart;
float fpart = modff(f, &ipart);
printf("%g is %s whole number.\n", f, fpart == 0.0 ? "a" : "not a");
Run Code Online (Sandbox Code Playgroud)

检测“整体性”的替代方法,用于"%g"打印输出减少的浮点数。

printf("%g\n", 2.0/3.0);  // 0.666667
printf("%g\n", 1234.0);   // 1234
Run Code Online (Sandbox Code Playgroud)

也许更进一步"%g"。使用足够宽的精度来显示右侧的任何非零数字,.并让说明符在答案是整数时去掉尾随的零。

printf("%.*g\n", FLT_DECIMAL_DIG, some_float);
printf("%.*g\n", DBL_DECIMAL_DIG, some_double);
Run Code Online (Sandbox Code Playgroud)


Rob*_*rtS 5

比较answerFe的返回值floorf()函数(头math.h) -floorf()因为你处理类型的变量float-与answer作为参数传递。

通过这样做,您可以证明是否answer包含偶数十进制值。

对于输出本身,使用精度说明符 (fe .0) 指定小数部分中的位数并将其放在%f转换说明符之间,例如%.0f

精度.0指定您不希望显示小数部分。

// 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);     
}
Run Code Online (Sandbox Code Playgroud)

旁注:

  1. 请注意,浮点运算通常不会提供预期的结果: 浮点运算是否被破坏? 因此,即使这种技术也可能不适用于每个用例。

  2. 始终检查输入函数的返回值,例如scanf()是否发生输入错误。

  3. 我用floorf(), but ceilf(), truncf()orroundf()也可以用来达到同样的效果。

  4. int main()已弃用。使用int main(void)它为 声明一个完整的原型main()

  5. 由于answer到达else身体时将为负,因此打印answer * -1将输出正值,因为- * - = +. 也许这不是故意的,所以这只是一个提示。