使用词法强制转换在'string to double'中设置双精度变量的精度
Set precision of a double variable in 'string to double' using lexical cast
我有一个值为"496934.079">的字符串。 我使用boost::lexical_cast ((将其转换为双精度变量(名为pVar(。
但是,转换后存储在 pVar 中的值是">496934">而不是"496934.079"。缺少小数点后的数字。我已经阅读了有关std::setprecision((的信息,用于在此处的输出窗口中以所需的精度写入双精度值。但它只显示具有给定精度的值,并且不会将其保存在具有给定先见之明的变量中。
但是我想完全按照字符串变量str中的原样存储双精度值,以便pVar中的值为"496934.079"。如何以与输入字符串相同的精度将值存储在变量中,以便可以使用该变量进行进一步处理?
我的代码片段是:
int func()
{
string str = "496934.079";
double pVar = boost::lexical_cast<double> (str);
cout << pVar << endl;
return 0;
}
编辑:从字符串到浮点数的转换是我正在开发的代码的一部分,它逐行解析文本文件中的值并将它们写入另一个文件(.pcd 格式(。我正在添加用于此转换的整个代码以供参考。
int txt2pcd()
{
ifstream is("input_text_file.txt"); //read input text file
string str;
using namespace boost::algorithm;
pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
while(getline(is, str)) //parsing the text file contents line-by-line
{
std::string s = str.c_str();
std::vector<std::string> result;
std::istringstream iss(s);
for(std::string s; iss >> s; )
result.push_back(s);
pcl::PointXYZ point;
point.x = boost::lexical_cast<double> (result[0]); //string to float conversion for adding the X coordinate of the point
point.y = boost::lexical_cast<double> (result[1]); //string to float conversion for adding the Y coordinate of the point
point.z = boost::lexical_cast<double> (result[2]); //string to float conversion for adding the Z coordinate of the point
point_cloud_ptr->points.push_back (point);
point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
point_cloud_ptr->height = 1;
}
pcl::io::savePCDFile ("output_pcd_file.pcd", *point_cloud_ptr, false); //this is where the converted float values are stored (with digits altered after decimal point when compared to text file)
return 0;
}
下面是文本文件中的几行示例和转换后的".pcd"文件中的相应行。
在文本文件中:
496934.999 5419547.239 265.179
496933.981 5419542.579 263.819
496934.891 5419545.399 264.849
496934.939 5419546.329 265.111
496934.829 5419544.489 264.781
在".pcd"文件中(由boost::lexical_cast ((生成的相应值(
496935 5419547 265.17899
496933.97 5419542.5 263.819
496934.91 5419545.5 264.849
496934.94 5419546.5 265.11099
496934.84 5419544.5 264.78101
请注意,输出".pcd"文件中的值要么四舍五入,要么更改小数点后的数字。这可能是什么原因呢?
boost::lexical_cast<double>(str)
已经返回完整值496934.079
作为double
。这个完整的值存储在pVar
中,并可用于进一步的处理。
string str = "496934.079";
double pVar = boost::lexical_cast<double>(str); // pVar now contains 496934.079
std::setprecision
不会影响存储的值 - 它只是设置显示的位数(小数点之前和之后(。默认显示精度为6
,因此:
std::cout << pVar << std::endl; // this prints "496934" (6 digits)
以更高的精度显示存储的值(例如。10
( 给出:
#include <iomanip>
std::cout << std::setprecision(10) << pVar << std::endl; // this prints "496934.079"
但重申一下:这些都不会影响存储的价值。pVar
仍然包含496934.079
.例如:
std::cout << (int) (pVar * 1000) << std::endl; // this displays 496934079
编辑
问题中更新的代码表明,实际上,float
用于存储值,而不是double
(ref。pcl::PointXYZ
(。
float
没有足够的精度来存储完整的值496934.079
- 相反,它将存储它可以表示的最佳近似值(496934.09375
(。浮点数和双精度有什么区别?
如果要确保可以保留全部值(未修改(,请确保坚持使用double
。
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 为电容式触摸传感器设置延迟以防止"double press"
- 使用词法强制转换在'string to double'中设置双精度变量的精度
- 将向量<double>设置为wxComboBox的选择值
- 为什么我无法将"type 'double'"设置为实体"type 'double *'?"
- 将double-all的二维数组设置为-1的最快方法
- 设置Complex Double的最大值来代替Infinity
- 在c++中设置double会得到奇怪的数字