• Welcome to Valhalla Legends Archive.
 

Newbie Question (I bet)

Started by Chriso, September 19, 2007, 07:48 AM

Previous topic - Next topic

Chriso

I need to get the second digit of an int?

e.g.
int number = 4321;

therefore i would need to get 3

Thanks.

Hdx

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

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

Yegg

The second method is probably the better way to go.

Chriso

Thanks, didn't think about using the remainder operator.

Chriso

#4
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.

Hdx

x = 123456;
d =  10000;
x/d = 12
12 % 10 = 2
what don't you get?
~Hdx

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

Chriso

Ahh, sorry didn't read that properly... Thanks, got it working now :)

Camel

Quote from: Hdx on September 19, 2007, 11:50 AM
x = 123456;
d =  10000;
x/d = 12
12 % 10 = 2
what don't you get?
~Hdx

d = Math.pow(10, Math.floor(Math.log(x)/Math.log(10))); would be more precise