我正在尝试将使用串行通信发送到我的 arduino uno 的字符串解析为浮点数。我想使用这些浮点数来设置 RGB LED 的颜色。我的问题是,它只会读取前 2 位数字。排序。假设我输入了 100。它只会出现到 10.00。如果是 231,它将出现在 23.00。奇怪的是,如果我输入 32.43,它会出现 32.4300。我不知道它为什么这样做。这是我的代码:
float led1Color[3];
...
for (int i = 0; i < 3; i++) {
int index = content.indexOf(",");
content = content.substring(index + 1); //removes additional text in front of numbers
led1Color[i] = atof(content.substring(0, index).c_str());
Serial.println(led1Color[i], 4);
}
现在假设我发送了以下内容:“RGBLED,43.61,52,231”。首先,移除 RGBLED。然后,控制台显示的 3 个值如下:
43.6100 52.0000 23.0000
显然这里的问题是我需要值 231,而不是 23.0000。我以前从未真正用 C/C++ 编程过,所以有什么我遗漏的吗?为什么三位数会变成二位数?