我与不幸使用库工作boost::lexical_cast从转换double到string。
我需要能够明确地反映我这一方面的行为,但是我希望这样做无需传播boost。
是否可以使用to_string,sprintf或标准中包含的其他功能来保证行为相同?
c++ boost standard-library number-formatting boost-lexicalcast
我有一个字符串,其值为"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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:从字符串到浮点数的转换是我正在开发的代码的一部分,它逐行解析文本文件中的值并将它们写入另一个文件(.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> …Run Code Online (Sandbox Code Playgroud)