是否有一个函数可以在C中对浮点进行取整,还是我需要自己编写?

是否有一个函数可以在C中对浮点进行取整,还是我需要自己编写?,c,floating-point,rounding,C,Floating Point,Rounding,是否有一个函数可以在C中对浮点进行取整,还是我需要自己编写 浮点数=45。592346543 我想将实际值四舍五入到小数点后一位,conver=45。6#include #include <math.h> double round(double x); float roundf(float x); 双圆(双x); 浮动圆F(浮动x); 别忘了与-lm链接。另请参见ceil()、floor()和trunc() 双圆(双x); 浮动圆F(浮动x); 别忘了与-lm链接。另请参见c

是否有一个函数可以在C中对浮点进行取整,还是我需要自己编写

浮点数=45。592346543

我想将实际值四舍五入到小数点后一位,conver=45。6

#include
#include <math.h>

double round(double x);
float roundf(float x);
双圆(双x); 浮动圆F(浮动x);
别忘了与-lm链接。另请参见ceil()、floor()和trunc() 双圆(双x); 浮动圆F(浮动x);
别忘了与-lm链接。另请参见ceil()、floor()和trunc()。

当然,您可以使用roundf()。如果要四舍五入到一个小数点,则可以执行如下操作:
roundf(10*x)/10
当然,可以使用roundf()。如果你想四舍五入到一个小数点,那么你可以做一些类似的事情:
roundf(10*x)/10
只是简单地概括一下Rob的答案,如果你没有在输出时这样做,你仍然可以使用与
sprintf()
相同的界面

不过,我认为还有另一种方法。您可以尝试
ceil()
floor()
上下取整。一个很好的技巧是加上0.5,所以任何超过0.5的都会向上取整,但低于0.5的都会向下取整
ceil()
floor()
只能在
double
上工作


编辑:另外,对于浮动,可以使用
truncf()
截断浮动。同样的+0.5技巧应该可以用于精确的四舍五入。

只是概括一下Rob的答案,如果您不在输出上使用它,您仍然可以使用与
sprintf()相同的接口。

float round_tenthB(float x) {
  const float limit = 1.0/FLT_EPSILON;
  if (fabsf(x) < limit) {
    return roundf(x*10.0f)/10.0f;
  }
  return x;
}
不过,我认为还有另一种方法。您可以尝试
ceil()
floor()
上下取整。一个很好的技巧是加上0.5,所以任何超过0.5的都会向上取整,但低于0.5的都会向下取整
ceil()
floor()
只能在
double
上工作


编辑:另外,对于浮动,可以使用
truncf()
截断浮动。同样的+0.5技巧应该可以实现精确的四舍五入。

正如Rob提到的,您可能只想将浮点打印到小数点后1位。在这种情况下,您可以执行以下操作:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  float conver = 45.592346543;
  printf("conver is %0.1f\n",conver);
  return 0;
}
float round_tenthB(float x) {
  const float limit = 1.0/FLT_EPSILON;
  if (fabsf(x) < limit) {
    return roundf(x*10.0f)/10.0f;
  }
  return x;
}
#包括
#包括
int main()
{
浮子对流=45.592346543;
printf(“conver为%0.1f\n”,conver);
返回0;
}
如果你想对存储的值进行四舍五入,那就有点复杂了。首先,您的一位小数表示法很少有精确的浮点模拟。如果你只是想尽可能接近,像这样的事情可能会奏效:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
  float conver = 45.592346543;
  printf("conver is %0.1f\n",conver);

  conver = conver*10.0f;
  conver = (conver > (floor(conver)+0.5f)) ? ceil(conver) : floor(conver);
  conver = conver/10.0f;

  //If you're using C99 or better, rather than ANSI C/C89/C90, the following will also work.
  //conver = roundf(conver*10.0f)/10.0f;

  printf("conver is now %f\n",conver);
  return 0;
}
#包括
#包括
#包括
int main()
{
浮子对流=45.592346543;
printf(“conver为%0.1f\n”,conver);
对流=对流*10.0f;
对流=(对流>(地板(对流)+0.5f))?天花板(对流):地板(对流);
对流=对流/10.0f;
//如果您使用的是C99或更好的版本,而不是ANSI C/C89/C90,那么以下内容也可以使用。
//conver=圆整度(conver*10.0f)/10.0f;
printf(“conver现在是%f\n”,conver);
返回0;
}

我怀疑这第二个例子是否是您想要的,但为了完整性,我加入了它。如果你确实需要以这种方式在内部表示你的数字,而不仅仅是在输出上,考虑使用A代替.

如ROB所提到的,你可能只想将浮点打印到1位小数点。在这种情况下,您可以执行以下操作:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  float conver = 45.592346543;
  printf("conver is %0.1f\n",conver);
  return 0;
}
#包括
#包括
int main()
{
浮子对流=45.592346543;
printf(“conver为%0.1f\n”,conver);
返回0;
}
如果你想对存储的值进行四舍五入,那就有点复杂了。首先,您的一位小数表示法很少有精确的浮点模拟。如果你只是想尽可能接近,像这样的事情可能会奏效:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
  float conver = 45.592346543;
  printf("conver is %0.1f\n",conver);

  conver = conver*10.0f;
  conver = (conver > (floor(conver)+0.5f)) ? ceil(conver) : floor(conver);
  conver = conver/10.0f;

  //If you're using C99 or better, rather than ANSI C/C89/C90, the following will also work.
  //conver = roundf(conver*10.0f)/10.0f;

  printf("conver is now %f\n",conver);
  return 0;
}
#包括
#包括
#包括
int main()
{
浮子对流=45.592346543;
printf(“conver为%0.1f\n”,conver);
对流=对流*10.0f;
对流=(对流>(地板(对流)+0.5f))?天花板(对流):地板(对流);
对流=对流/10.0f;
//如果您使用的是C99或更好的版本,而不是ANSI C/C89/C90,那么以下内容也可以使用。
//conver=圆整度(conver*10.0f)/10.0f;
printf(“conver现在是%f\n”,conver);
返回0;
}
我怀疑这第二个例子是否是您想要的,但为了完整性,我加入了它。如果你确实需要以这种方式在内部表示你的数字,而不仅仅是在输出上,请考虑使用一个Addio.< /P> < P>有一个<代码>圆()/<代码>函数,也就是“代码> Frand())//C>,它将以最接近的整数表示为一个双。但那不是你想要的

我也有同样的问题,我写了这样的话:

#include <math.h>

   double db_round(double value, int nsig)
/* ===============
**
** Rounds double <value> to <nsig> significant figures.  Always rounds
** away from zero, so -2.6 to 1 sig fig will become -3.0.
**
** <nsig> should be in the range 1 - 15
*/

{
    double     a, b;
    long long  i;
    int        neg = 0;


    if(!value) return value;

    if(value < 0.0)
    {
        value = -value;
        neg = 1;
    }

    i = nsig - log10(value);

    if(i) a = pow(10.0, (double)i);
    else  a = 1.0;

    b = value * a;
    i = b + 0.5;
    value = i / a;

    return neg ? -value : value;
} 
#包括
双db_四舍五入(双值,整数nsig)
/* ===============
**
**四舍五入到有效数字。总是绕来绕去
**远离零,因此-2.6到1 sig fig将变为-3.0。
**
**应在1-15范围内
*/
{
双a,b;
龙龙我;
int-neg=0;
if(!value)返回值;
如果(值<0.0)
{
值=-值;
负=1;
}
i=nsig-对数10(值);
如果(i)a=功率(10.0,(双)i);
否则a=1.0;
b=值*a;
i=b+0.5;
值=i/a;
返回负?-值:值;
} 
有一个
round()
函数,也是
fround()
,它将舍入到表示为双精度的最接近整数。但那不是你想要的

我也有同样的问题,我写了这样的话:

#include <math.h>

   double db_round(double value, int nsig)
/* ===============
**
** Rounds double <value> to <nsig> significant figures.  Always rounds
** away from zero, so -2.6 to 1 sig fig will become -3.0.
**
** <nsig> should be in the range 1 - 15
*/

{
    double     a, b;
    long long  i;
    int        neg = 0;


    if(!value) return value;

    if(value < 0.0)
    {
        value = -value;
        neg = 1;
    }

    i = nsig - log10(value);

    if(i) a = pow(10.0, (double)i);
    else  a = 1.0;

    b = value * a;
    i = b + 0.5;
    value = i / a;

    return neg ? -value : value;
} 
#包括
双db_四舍五入(双值,整数nsig)
/* ===============
**
**四舍五入到有效数字。总是绕来绕去
**远离零,因此-2.6到1 sig fig将变为-3.0。
**
**应在1-15范围内
*/
{
双a,b;
龙龙我;
int-neg=0;
if(!value)返回值;
如果(值<0.0)
{
值=-值;
负=1;
}
i=nsig-对数10(值);
如果(i)a=功率(10.0,(双)i);
其他a