Getting the second digit from a long variable

shree :

I am trying to fetch second digit from a long variable.

long mi = 110000000;
int firstDigit = 0;
String numStr = Long.toString(mi);
for (int i = 0; i < numStr.length(); i++) {
    System.out.println("" + i + " " + numStr.charAt(i));
    firstDigit =  numStr.charAt(1);
}

When I am printing firstDigit = numStr.charAt(1) on console. I am getting 1 which is expected but when the loop finishes firstDigit has 49. Little confused why.

Ankit jain :

You got confused because 49 is ASCII value of integer 1. So you may parse character to integer then you can see integer value.

    Integer.parseInt(String.valueOf(mi).charAt(1)):

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=436991&siteId=1