我正在particle.io火花平台上工作,目前我正试图打印一个花车作为一个字符串。我看到了一些解决方案,您可以使用字符串流来转换浮点数。
我的执行情况如下:
#include <sstream>
void loop()
{
float tempC = 21.35;
std::ostringstream stream;
stream << tempC;
std::string tempCString = stream.str();
// why does this give me a blank string?
Serial.print("Temp 1: ");
Serial.println(tempCString.c_str());
// while this outputs the float
Serial.print("Temp 2: ");
Serial.println(tempC);
Serial.println(tempCString.size());
}这将产生以下结果:
Temp 1:
Temp 2: 21.35
6此外,这也无法编译:
Serial.println(tempCString);有以下错误:
error: no matching function for call to 'USBSerial::println(std::string&)'编辑:链接到关于Serial.print的粒子文档
发布于 2016-10-16 05:22:07
我找到了答案。不确定这对于常规c++是否适用,但是对于运行在粒子火花上的c++版本,解决方案如下:
float myFloat = 6.123;
String floatString(myFloat, 2);这给了我一个可以使用的字符串!
https://stackoverflow.com/questions/40015287
复制相似问题