Author Topic: Splitting Two Digit Number in AVRstudio  (Read 1657 times)

0 Members and 1 Guest are viewing this topic.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Splitting Two Digit Number in AVRstudio
« 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
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Splitting Two Digit Number in AVRstudio
« Reply #1 on: September 06, 2009, 12:41:26 AM »
Mod it, or %. Mod is basically what the remainder is.

17 % 10 = 7
9 % 2 = 1

Offline Pratheek

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 3
    • Probots
Re: Splitting Two Digit Number in AVRstudio
« Reply #2 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;

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Splitting Two Digit Number in AVRstudio
« Reply #3 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

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Splitting Two Digit Number in AVRstudio
« Reply #4 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
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

 


Get Your Ad Here