Society of Robots - Robot Forum
Software => Software => Topic started by: airman00 on September 05, 2009, 10:09:18 PM
-
I read somewhere that AVRstudio rounds down numbers, so if I have 9.7, it will actually turn to 9. Is that correct?
Because I want to split a two digit number into two digits.
Is there a different method I can do?
Code:
int temp;
temp = Double Digits/10
temp*10 = First_digit
second_digit = Double_digits- first_digit
Example:
Double_digits = 15
15/10 = 1.5 , rounds down to 1 .
First digit = 1*10
Second_digit = 15 -10 = 5
-
Mod it, or %. Mod is basically what the remainder is.
17 % 10 = 7
9 % 2 = 1
-
Like razor concepts said use mod operator.
Someting like this
int tmp = double_digits;
first_digit = tmp%10;
tmp /= 10;
second_digit = tmp;
-
I read somewhere that AVRstudio rounds down numbers, so if I have 9.7, it will actually turn to 9. Is that correct?
Nope, its the compiler (in this case gcc) that does this. All programming languages to my knowledge do this for int types. :P
-
Thanks all!
This works:
digit_1 = double_digits / 10; // left most digit
digit_2 = double_digits % 10; // right most digit