Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 digits of integers
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
iluvchairs112




PostPosted: Tue May 22, 2007 7:47 pm   Post subject: digits of integers

if you have x number of digits in a number, how can you go about finding the value of one of the digit (for example, the digit in the 100s place)?
i thought about somehow using division, but i'm not sure how that would work
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Tue May 22, 2007 8:15 pm   Post subject: Re: digits of integers

Do an integer division by 1 for ones column, 10 for tens column, 1000 for thousands column, etc. and then modulus by 10.

Java:
int a = 123645;
//output the 10 000's digit
System.out.println(a / 10000 % 10); //=>2
iluvchairs112




PostPosted: Tue May 22, 2007 8:20 pm   Post subject: RE:digits of integers

would that not result in a decimal place? i'm looking for just the individual digit not a decimal number
PaulButler




PostPosted: Tue May 22, 2007 8:41 pm   Post subject: RE:digits of integers

Another way is to pull the digit from the string. If the number is not an integer you will get bad results though.

Java:

int c = 38843243;
int b = Integer(String.valueOf(c).charAt(4));
// Should set b = 3?


Note that the digit number you give will be counted from the left, not the right.
richcash




PostPosted: Tue May 22, 2007 8:51 pm   Post subject: Re: digits of integers

iluvchairs112 wrote:

would that not result in a decimal place? i'm looking for just the individual digit not a decimal number

My method will output the appropriate digit, I used modulus. Wink


This is more generalized. It works for doubles too.
Java:

double a = 1234567.1234567;
//10 000's digit
System.out.println((int) (a / 10000 % 10)); //=> outputs 3
//hundred-thousandTH digit
System.out.println((int) (a / 0.00001 % 10));//=> outputs 5


@PaulButler : Yeah, I think your method works, but doesn't it take more computer power to convert an integer into a string than to do multiplication and division?
PaulButler




PostPosted: Tue May 22, 2007 9:01 pm   Post subject: Re: digits of integers

richcash @ Tue May 22, 2007 8:51 pm wrote:

@PaulButler : Yeah, I think your method works, but doesn't it take more computer power to convert an integer into a string than to do multiplication and division?


Not only that, your way makes more sense mathematically. I just threw my method out there in case iluvchairs was more familiar with strings than modulus.
richcash




PostPosted: Tue May 22, 2007 10:56 pm   Post subject: Re: digits of integers

PaulButler wrote:

Java:

int c = 38843243;
int b = Integer(String.valueOf(c).charAt(4));
// Should set b = 3?



Actually, that doesn't work. Your Integer should be Integer.valueOf() and even then the charAt method returns a char, which when converted into an int results in its ASCII value. Therefore, 51 is outputted. Wink

iluvchairs112, if you want to use the string way of doing it you'll have to do something like this :

Java:
int c = 38843243;
String b = String.valueOf(c).substring(1, 2);
int a = Integer.valueOf(b);
System.out.println(a);


But it's still better to use the math way! Smile
PaulButler




PostPosted: Wed May 23, 2007 5:47 am   Post subject: RE:digits of integers

Oh yeah, that's right. Here is another way to do it with the string method, but it is quite gross.

Java:

int c = 38843243;
int b = Integer.valueOf(String.valueOf(String.valueOf(c).charAt(4)));
// Should set b = 3?


iluvchairs, you might as well use richcash's way. If you don't understand modulus, it would be a good time to look it up in the Java documentation so you know what the code does.

Here is a method to do it for any arbitrary number and digit (untested; richcash's method):

Java:

public int digitAt(double number, int digit){
    return ((number / 10^digit) % 10);
}
Sponsor
Sponsor
Sponsor
sponsor
iluvchairs112




PostPosted: Wed May 23, 2007 7:15 pm   Post subject: RE:digits of integers

how about a sum of the x number of digits? so let's say the int was ABCDEFG, how would you go about getting the sum of A+B+C+D+E+F+G?
would you have to make a new variable for each digit?
richcash




PostPosted: Wed May 23, 2007 7:46 pm   Post subject: Re: digits of integers

Java:
int num = 38643243;
int sum = 0;
for (int i = 0; i < (int) Math.log(num) + 1; i ++) {
        sum += (num / Math.pow(10, i)) % 10;
}

Don't forget to import java.lang.Math Wink

(int) Math.log(num) + 1
this will compute how many digits are in the number. If x has two digits, log(x) will be between 1 and 2. If x has five digits, the log will be between 4 and 5. So, now do you see how it works and why we floor the result and add 1?

num / Math.pow(10, i)
divide the number by ten^digit (1 for ones column, ten for tens column, 1000 for thousands column, etc.) to bring the digit we want to the ones column

% 10
the result of num / ten^digit will bring the digit we want to the ones column, and then we get it out by doing modulus 10 (look up modulus for more info)

Feel free to ask any questions. Smile


[edit] Note I fixed a mistake in my explanation for the log(x). [/edit]
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: