2017-05-21 129 views
0

我正在創建檢查溫度並將數據發送到Xively protal的arduino項目。我找到了一些例子,但我不明白傳感器讀數方法中的位數。任何人都可以向我解釋嗎?尤其是紅利/ 10的部分?計算傳感器讀數中的位數(Arduino)

的方法是:

//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; 
} 

我真的很感激任何解釋

回答

1

肯定。如果我的號碼是1234,我想知道有多少位數?那麼我從1開始,因爲我知道至少有1。然後我除以10,這給了我123.這是大於0,所以我知道至少有一個數字。然後我除以十,這給了我12,這是大於十,所以我知道至少有一個數字。再次除以10,我得到1.這是大於0,所以這又是一個數字。現在我知道我已經把1234中的所有數字都計算在內了。

基本上你使用十進制除法來除去數字的最後一位數字。如果那留下你仍然有一個數字,那麼有更多的數字。一遍又一遍,直到你達到0.一旦你達到0,你已經咀嚼他們,並完成計數。

這只是數學,不是任何編程的深奧。