Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified
Accepted Solution

Convert int to; int for ones, int for tens, int for onehundereds

I should know this from basic programming, but I'm having a complete mind blank.

 

What is the easiest way to take an integer like 125 and break it out into an integer for the hundreds place, the tens place and ones place.  For example 125 would be:

 

int Hundreds = 1

int Tens = 2

int Ones = 5

 

Thanks!

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Convert int to; int for ones, int for tens, int for onehundereds

Many ways to do this. One simple way is to convert to a String and break it up this way. Other ways involve doing some simple mathematics with modulo, division, and taking integer parts (eg., if you want the kth digit of N, integer_part((N % 10^k)/10^(k-1)) does the trick)

 

There is nothing Blackberry specific that handles this.

Highlighted
Developer
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Convert int to; int for ones, int for tens, int for onehundereds

I was thinking about the convert to string method and using toCharArray(). Then I can take each seperate index for the character array and convert to integer.

 

But your mathmatic solution takes less conversion and variables, I'll think I'll use you formula.

 

 

Thanks for the input!