C语言 小数点后N位C四舍五入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14003487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:49:54  来源:igfitidea点击:

Round in C with N digits after decimal point

cmathrounding

提问by testCoder

Possible Duplicate:
Rounding Number to 2 Decimal Places in C

可能的重复:
在 C 中将数字四舍五入到 2 个小数位

I have not found a function with a signature double round(double d, int digits)like herein c. When i try to build i get a error:

我还没有在 c 中找到具有double round(double d, int digits)这里这样的签名的函数。当我尝试构建时,出现错误:

error: too many arguments to function 'round'

错误:函数“round”的参数太多

How can I round in C with N digits after the decimal point?

如何在小数点后用 N 位在 C 中舍入?

回答by andand

Using recursion (which is going to be slow for some values of digits)

使用递归(对于某些数字值会很慢)

#include <math.h>
double my_round(double x, unsigned int digits) {
  if (digits > 0) {
    return my_round(x*10.0, digits-1)/10.0;
  }
  else {
    return round(x);
  }
}

A method likely to be somewhat faster, but which relies on a single call to the slow powfunction:

一种可能会更快一些的方法,但它依赖于对慢速pow函数的单个调用:

#include <math.h>

double my_round(double x, unsigned int digits) {
    double fac = pow(10, digits);
    return round(x*fac)/fac;
}

An even faster method is to precompute a lookup table with the likely powers and use that instead of pow.

一种更快的方法是预先计算具有可能幂的查找表并使用它而不是pow.

#include <math.h>

double fac[];  // population of this is left as an exercise for the reader

double my_round(double x, unsigned int digits) {
    return round(x*fac[digits])/fac[digits];
}

回答by Muhammad

here's a (very) simple function,

这是一个(非常)简单的函数,

double round1(double num, int N) {
      int temp=(int) num*pow(10,N); 
      double roundedN= temp/pow(10,N);
      return roundedN;
}

回答by md5

In C standard, such function does not exist. Anyway, you can write your own.

在 C 标准中,这样的函数是不存在的。不管怎样,你可以自己写。

#include <math.h>

/* Round `n` with `c` digits after decimal point. */

double nround (double n, unsigned int c)
{
    double marge = pow (10, c);
    double up    = n * marge;
    double ret   = round (up) / marge;

    return ret;
}

See also the comments above about floating points "decimal point".

另请参阅上面关于浮点“小数点”的评论。

回答by Mats Petersson

Whilst "answerd" gives a decent answer, here's one that works for arbitrarily large numbers:

虽然“已回答”给出了一个不错的答案,但这里有一个适用于任意大数的答案:

double round1(double num, int N) {
      ASSERT(N > 0);
      double p10 = pow(10,N);
      return round(num* p10) / p10;
}

Of course, as stated, floating point numbers don't have a set number of decimal digits, and this is NOT guaranteed to PRINT as 3.70000 if you call printf("%8.5f", round1(3.7519, 1));for example.

当然,如前所述,浮点数没有固定的十进制数字,printf("%8.5f", round1(3.7519, 1));例如,如果您打电话,则不能保证打印为 3.70000 。