计算传感器读数的位数(Arduino)
我正在创建arduino项目,该项目检查温度并将数据发送给Xively protal。我找到了一些例子,但我不了解传感器读取方法中的位数。谁能给我解释一下吗?特别是有股息/10的部分 方法是:计算传感器读数的位数(Arduino),arduino,Arduino,我正在创建arduino项目,该项目检查温度并将数据发送给Xively protal。我找到了一些例子,但我不了解传感器读取方法中的位数。谁能给我解释一下吗?特别是有股息/10的部分 方法是: //This method calulates the number of digits in the sensor readind //Since each digit of the ASCII decimal representation is a byte, the number //of digi
//This method calulates the number of digits in the sensor readind
//Since each digit of the ASCII decimal representation is a byte, the number
//of digits equals the numbers of bytes:
int getLength(int someValue)
{
//there's at least one byte:
int digits = 1;
//continually divide the value by ten, adding one to the digit count for
//each time you divide, until you are at 0
int getLength(int someValue) {
int digits = 1;
int dividend = someValue /10 ;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
return digits;
}
如果您能给我解释一下,我将不胜感激。如果我有号码1234,我想知道有多少位数字?我从1开始,因为我知道至少有1个。然后我除以10,得到123。这大于0,所以我知道至少还有一个数字。然后我除以10,得到12,大于10,所以我知道至少还有一个数字。再除以10,我得到1。这大于0,所以多了一个数字。再除以10,我得到0。现在我知道我已经数完了1234中的所有数字 基本上,您使用的是除以10来删除数字的最后一位。如果你仍然有一个数字,那么还有更多的数字。反复这样做直到你达到0。一旦你到了0,你就把它们都嚼了,数完了
这只是数学,不是任何编程的秘方 >添加到@ DeltAgg的答案,这里是C++中可视化的解决方案⤵︎
int currentNumberOfDigits=0;
整数=439348;
while(数字!=0){
数字=数字/10;
currentNumberOfDigits++;
}
返回currentNumberOfDigits;//6位数字
这是使用字符串的一个很好的替代方法,因为这样您可以在Arduino上使用更少的存储空间,因为您可以使用其他数据类型而不是字符串
下面是其他数据类型大小的示例⤵︎
回答得真棒!非常感谢。为了向任何人澄清,请确保如果您使用的是浮点或双精度浮点,那么您要查找的是最终的数字
<1,而不是相等的!