返回双用precision [英] Returning double with precision
问题描述
说我有返回双击
的方法,但我想确定precision要返回的值的点之后。我不知道双击
varaible的价值。
Say I have a method returning a double
, but I want to determine the precision after the dot of the value to be returned. I don't know the value of the double
varaible.
例如:
double i = 3.365737;
return i;
我想返回值与点后的3号precision
I want the return value to be with precision of 3 number after the dot
含义:返回值是 3.365
另外一个例子:
double i = 4644.322345;
return i;
我要的返回值是: 4644.322
推荐答案
你想要的是十进制数字截断了一定数字之后。您可以轻松地做到这一点与地板
从功能<文件math.h>
(或性病::地板
从<&CMATH GT;
如果你使用C ++):
What you want is truncation of decimal digits after a certain digit. You can easily do that with the floor
function from <math.h>
(or std::floor
from <cmath>
if you're using C++):
double TruncateNumber(double In, unsigned int Digits)
{
double f=pow(10, Digits);
return ((int)(In*f))/f;
}
不过,我认为,在某些情况下,你可能会得到一些奇怪的结果(最后一位数字是一个比开/关)由于如何浮点内部工作原理。
Still, I think that in some cases you may get some strange results (the last digit being one over/off) due to how floating point internally works.
在另一方面,大多数时候你只要通过周围的双击
因为是和甲流,这与正确的自动完成输出时,它只截断流标志。
On the other hand, most of time you just pass around the double
as is and truncate it only when outputting it on a stream, which is done automatically with the right stream flags.
这篇关于返回双用precision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!