问题描述
嗨
有没有人知道,如何用特定数字对双倍值进行舍入
小数点后
位数?
这样的函数:
RoundMyDouble(double& value,short numberOfPrecisions)
然后更新小数点后带numberOfPrecisions的值
点。
感谢任何帮助。
谢谢。
md
Hi
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
A function like this:
RoundMyDouble (double &value, short numberOfPrecisions)
It then updates the value with numberOfPrecisions after the decimal
point.
Any help is appreciated.
Thanks.
md
推荐答案
{
int p = pow(10,numberOfPrecisions);
value =(int)(value * p + 0.5)/(双)p;
}
{
int p = pow(10, numberOfPrecisions);
value = (int)(value * p + 0.5) / (double)p;
}
您通常不能将双倍值舍入到特定数量的
位数,因为双打不是小数。保存你的舍入'直到你
准备将数字格式化为十进制字符串。
参见每个计算机科学家应该知道的关于浮动的内容 - 点
算术
You cannot in general round a double value to a specific number of
digits, because doubles are not decimal. Save your rounding ''til you
are ready to format the number into a decimal string.
See "What Every Computer Scientist Should Know About Floating-Point
Arithmetic" http://docs.sun.com/source/806-3568/ncg_goldberg.html
嗯,你有一个语法错误:double& value不合法。你
大概意味着双倍的价值。
Elsethread,你得到了这个建议(经过适当的修改,以便它实际上是
)编译,并添加了一个驱动程序):
#include< stdio.h>
#include< math.h>
void RoundMyDouble(double * value,short numberOfPrecisions)
{
int p = pow(10,numberOfPrecisions);
* value =(int)(* value * p + 0.5)/(double)p;
}
int main( void)
{
double v = 3.14159265358979323846;
short int ndp = 0;
while(ndp< ; 10)
{
double newv = v;
RoundMyDouble(& newv,ndp);
printf("%。16f,\" rounded\"到%hd小数,是%。16f \ n",
v,
ndp ++,
newv);
}
返回0;
}
以下是测试结果:
3。 1415926535897931,圆形到0位小数,是3.0000000000000000
3.1415926535897931," round"到1位小数,是3.1000000000000001
3.1415926535897931,圆形到2位小数,是3.1400000000000001
3.1415926535897931,舍入到3位小数,是3.1419999999999999
3.1415926535897931,舍入到4位小数,是3.1415999999999999
3.1415926535897931,舍入到5位小数,是3.1415899999999999
3.1415926535897931,舍入至6位小数,是3.1415929999999999
3.1415926535897931,圆形到7位小数,是3.1415926999999999
3.1415926535897931,舍入到8位小数,是3.1415926500000002
3.1415926535897931,舍入到9位小数,是-2.1474836480000001
正如你所看到的,它根本不是圆的。它将价值接近
到需要的值,但是没有达到要求的正确。
最后的结果看起来很有趣,不是吗? :-)
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
Well, you have a syntax error right there: double &value isn''t legal. You
presumably meant double *value.
Elsethread, you were given this suggestion (suitably modified so that it
will actually compile, and with a driver added):
#include <stdio.h>
#include <math.h>
void RoundMyDouble (double *value, short numberOfPrecisions)
{
int p = pow(10, numberOfPrecisions);
*value = (int)(*value * p + 0.5) / (double)p;
}
int main(void)
{
double v = 3.14159265358979323846;
short int ndp = 0;
while(ndp < 10)
{
double newv = v;
RoundMyDouble(&newv, ndp);
printf("%.16f, \"rounded\" to %hd decimals, is %.16f\n",
v,
ndp++,
newv);
}
return 0;
}
Here are the test results:
3.1415926535897931, "rounded" to 0 decimals, is 3.0000000000000000
3.1415926535897931, "rounded" to 1 decimals, is 3.1000000000000001
3.1415926535897931, "rounded" to 2 decimals, is 3.1400000000000001
3.1415926535897931, "rounded" to 3 decimals, is 3.1419999999999999
3.1415926535897931, "rounded" to 4 decimals, is 3.1415999999999999
3.1415926535897931, "rounded" to 5 decimals, is 3.1415899999999999
3.1415926535897931, "rounded" to 6 decimals, is 3.1415929999999999
3.1415926535897931, "rounded" to 7 decimals, is 3.1415926999999999
3.1415926535897931, "rounded" to 8 decimals, is 3.1415926500000002
3.1415926535897931, "rounded" to 9 decimals, is -2.1474836480000001
As you can see, it doesn''t really round at all. It nudges the value close
to what is required, but doesn''t hit the requirement right on the nose.
And that last result looks like a lot of fun, doesn''t it? :-)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
这篇关于四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!