Society of Robots - Robot Forum

Software => Software => Topic started by: airman00 on September 05, 2009, 10:09:18 PM

Title: Splitting Two Digit Number in AVRstudio
Post 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:
Code: [Select]
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
Title: Re: Splitting Two Digit Number in AVRstudio
Post by: Razor Concepts on September 06, 2009, 12:41:26 AM
Mod it, or %. Mod is basically what the remainder is.

17 % 10 = 7
9 % 2 = 1
Title: Re: Splitting Two Digit Number in AVRstudio
Post by: Pratheek on September 06, 2009, 01:08:35 AM
Like razor concepts said use mod operator.
Someting like this

int tmp = double_digits;
first_digit = tmp%10;
tmp /= 10;
second_digit = tmp;
Title: Re: Splitting Two Digit Number in AVRstudio
Post by: Admin on September 06, 2009, 08:28:00 AM
Quote
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
Title: Re: Splitting Two Digit Number in AVRstudio
Post by: airman00 on September 06, 2009, 08:42:09 AM
Thanks all!

This works:

   digit_1 = double_digits / 10;  // left most digit
   digit_2 = double_digits % 10;  // right most digit