在C ++中,如何在不丢失float64中的任何数据的情况下将float64类型的数据转换为字符串?我不仅需要将其转换为字符串,还需要在数字的任一侧添加一个字符串,然后将其发送以写入文件中。
码:
string cycle("---NEW CYCLE ");
cycle+=//convert float64 to string and add to cycle
cycle+= "---\r\n";
writeText(cycle.c_str()); //writes string to txt file
谢谢。
最佳答案
您应该使用sprintf
。请参见C++ Reference此处的文档。
举个例子:
char str[30];
float flt = 2.4567F;
sprintf(str, "%.4g", flt );
另外,我会使用
string::append
添加字符串。请参见here。更新
根据注释更新了代码。
关于c++ - float 到字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9590990/