Valhalla Legends Forums Archive | Java Programming | Newbie Question (I bet)

AuthorMessageTime
DDA-TriCk-E
I need to get the second digit of an int?

e.g.
int number = 4321;

therefore i would need to get 3

Thanks.
September 19, 2007, 12:48 PM
HdxBmx27
Theres a couple ways you can do this, both are kinda eww.
But 1) Convert it to a string and then use .charAt(1);
Or 2) / x % 10, where X is 1 less signifigance then the int (If it was 1000, x = 100, num = 100, x=10, etc..)
~Hdx
September 19, 2007, 2:09 PM
Yegg
The second method is probably the better way to go.
September 19, 2007, 4:03 PM
DDA-TriCk-E
Thanks, didn't think about using the remainder operator.
September 19, 2007, 4:41 PM
DDA-TriCk-E
Hmm, I actually need to get the second number ONLY, not the trailing numbers as well. Any ideas? I don't want to convert it to a String that looks too inefficient.
September 19, 2007, 4:47 PM
HdxBmx27
x = 123456;
d =  10000;
x/d = 12
12 % 10 = 2
what don't you get?
~Hdx
September 19, 2007, 4:50 PM
DDA-TriCk-E
Ahh, sorry didn't read that properly... Thanks, got it working now :)
September 19, 2007, 4:54 PM
Camel
[quote author=Hdx link=topic=17030.msg172933#msg172933 date=1190220629]
x = 123456;
d =  10000;
x/d = 12
12 % 10 = 2
what don't you get?
~Hdx
[/quote]

d = Math.pow(10, Math.floor(Math.log(x)/Math.log(10))); would be more precise
September 19, 2007, 5:51 PM

Search