错误消息:二进制"运算符%"类型"float"和"int"的操作数无效



环境:Arduino IDE(v1.8.13(

在我的一个项目中,我试图使用浮点数据类型显示十进制温度值(temp(。这段代码适用于int数据类型。有谁能帮我解决这个错误吗;二进制"operator%"的类型为"float"one_answers"int"的操作数无效;。我是编程的初学者

作为参考,我做了这个故障排除点击这里和点击这里

1:Switch语句中的浮点数据类型,但找不到特定的解决方案。

提前感谢!

完整程序:

switch (current_digit)
{
case 1:
disp((temp / 100) % 10);
digitalWrite(Dig1, LOW);  // turn on digit 1
break;
case 2:
disp( (temp / 10) % 10);   // prepare to display digit 2
digitalWrite(SegDP, LOW);  // print decimal point ( . )
digitalWrite(Dig2, LOW);   // turn on digit 2
break;
case 3:
disp(temp % 10);   // prepare to display digit 3
digitalWrite(Dig3, LOW);    // turn on digit 3
}
current_digit = (current_digit % 3) + 1;
}
}
如前所述,%适用于整数类型。

要获得值的第一个小数位,请从值中减去四舍五入的数字。将差值乘以10。通过使用floor函数或强制转换为integer,可以去掉小数。

简化示例:

12.5... -> 12
12.5... - 12 -> 0.5...
0.5... * 10 -> 5.00...
5.0... -> 5

或者将您的值转换为char数组。这将使您能够方便地访问每个数字。更改显示函数,使其采用char值。

最新更新