本文介绍了精确返回双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个方法返回一个 cd> cd> cd>

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.

另一方面,大多数时间,你只是传递 double ,只是在流上输出时截断它,这是使用正确的流标志自动完成。

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.

这篇关于精确返回双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:08