U
user
say i got an int, 530: what could i do to get back 2nd digit [the 3 in
this case]?
this case]?
user said:say i got an int, 530: what could i do to get back 2nd digit
[the 3 in this case]?
Stefan said:user said:say i got an int, 530: what could i do to get back 2nd digit
[the 3 in this case]?
Numbers do not have digits. Numerals might have digits.
A numeral is a numeric literal, i.e., a textual representation
of a number in specific notation (numeral system). However, a
numeral system has to be chosen, for example, a place-value
system, for example, the common decimal place-value system.
The meaning of »2nd digit« depends on where you start to count
the digits from: from the left or from the right. In the
special case of the numeral »530« this does not matter, but
for the numeral »5320« it might make a difference.
user said:would the easiest way be to change the int into a string, break the
string up, then give back the interger representation of the digit im
looking for?
if the number was 5320, and it was counted left to right, id ask for
the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt
matter what direction its counted from, i just need some way of getting
that value. by saying that im confusing the definition of numbers and
numerals does not help me get the value i need.
would the easiest way be to change the int into a string, break the
string up, then give back the interger representation of the digit im
looking for?
Stefan said:user said:say i got an int, 530: what could i do to get back 2nd digit
[the 3 in this case]?
Numbers do not have digits. Numerals might have digits.
A numeral is a numeric literal, i.e., a textual representation
of a number in specific notation (numeral system). However, a
numeral system has to be chosen, for example, a place-value
system, for example, the common decimal place-value system.
The meaning of »2nd digit« depends on where you start to count
the digits from: from the left or from the right. In the
special case of the numeral »530« this does not matter, but
for the numeral »5320« it might make a difference.
if the number was 5320, and it was counted left to right, id ask for
the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt
matter what direction its counted from, i just need some way of getting
that value. by saying that im confusing the definition of numbers and
numerals does not help me get the value i need.
would the easiest way be to change the int into a string, break the
string up, then give back the interger representation of the digit im
looking for?
Stefan said:user said:say i got an int, 530: what could i do to get back 2nd digit
[the 3 in this case]?
Numbers do not have digits. Numerals might have digits.
A numeral is a numeric literal, i.e., a textual representation
of a number in specific notation (numeral system). However, a
numeral system has to be chosen, for example, a place-value
system, for example, the common decimal place-value system.
The meaning of »2nd digit« depends on where you start to count
the digits from: from the left or from the right. In the
special case of the numeral »530« this does not matter, but
for the numeral »5320« it might make a difference.
How about this. I wasn't sure if you wanted a String or an int...so I
did both.
int number = 530;
String sNumber = String.valueOf(number);
String sFirstDigit = sNumber.substring(0,1);
String sSecondDigit = sNumber.substring(1,2);
String sThirdDigit = sNumber.substring(2,3);
int firstDigit = Integer.parseInt(sFirstDigit);
int secondDigit = Integer.parseInt(sSecondDigit);
int thirdDigit = Integer.parseInt(sThirdDigit);
System.out.println(sFirstDigit + " " + sSecondDigit + " " +
sThirdDigit);
System.out.println(firstDigit + " " + secondDigit + " " + thirdDigit);
Output:
5 3 0
5 3 0
if the number was 5320, and it was counted left to right, id ask for
the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt
matter what direction its counted from, i just need some way of getting
that value. by saying that im confusing the definition of numbers and
numerals does not help me get the value i need.
would the easiest way be to change the int into a string, break the
string up, then give back the interger representation of the digit im
looking for?
Stefan said:say i got an int, 530: what could i do to get back 2nd digit
[the 3 in this case]?
Numbers do not have digits. Numerals might have digits.
A numeral is a numeric literal, i.e., a textual representation
of a number in specific notation (numeral system). However, a
numeral system has to be chosen, for example, a place-value
system, for example, the common decimal place-value system.
The meaning of »2nd digit« depends on where you start to count
the digits from: from the left or from the right. In the
special case of the numeral »530« this does not matter, but
for the numeral »5320« it might make a difference.
user said:if the number was 5320, and it was counted left to right, id
ask for the 2nd digit,
public class Main
{ public void run( final java.lang.String[] args )
{ java.lang.System.out.println( 5320 / 100 % 10 );
java.lang.System.out.println( Integer.toString( 5320 ).charAt( 1 )); }
public static void main( final java.lang.String[] args )
{ new Main().run( args ); }}
{ int pot[] = new int[]{ 1, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000 };
java.lang.System.out.println( 5320 / pot[ 2 ]% 10 ); }
{ int pot[] = new int[]{ 1, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000 };
java.lang.System.out.println( 5320 / pot[ 2 ]% 10 ); }
user said:say i got an int, 530: what could i do to get back 2nd digit [the 3 in
this case]?
user said:if the number was 5320, and it was counted left to right, id ask for
the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt
matter what direction its counted from, i just need some way of getting
that value. by saying that im confusing the definition of numbers and
numerals does not help me get the value i need.
user said:that sounds like it could work. if i divided by 10 twice, and got the
remainder of 930, i would have the "2nd" digit. is there a method that
lets me get the remainder of a number diving into another??
user said:say i got an int, 530: what could i do to get back 2nd digit [the 3 in
this case]?
user said:say i got an int, 530: what could i do to get back 2nd digit [the 3 in
this case]?
Lasse Reichstein Nielsen said:int magnitude = (int) Math.floor(Math.log10(number));
return ((int)(number / Math.pow(10, magnitude - 1))) % 10;
user said:say i got an int, 530: what could i do to get back 2nd digit [the 3 in
this case]?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.