digits of integers
Author |
Message |
iluvchairs112
|
Posted: 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
|
|
|
richcash
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
Posted: 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.
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
|
Posted: 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
|
Posted: 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.
iluvchairs112, if you want to use the string way of doing it you'll have to do something like this :
But it's still better to use the math way! |
|
|
|
|
|
PaulButler
|
Posted: 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
|
|
|
iluvchairs112
|
Posted: 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
|
Posted: 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
(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.
[edit] Note I fixed a mistake in my explanation for the log(x). [/edit] |
|
|
|
|
|
|
|