Skip to content

Instantly share code, notes, and snippets.

@NrI3
Created June 17, 2015 18:57
Show Gist options
  • Save NrI3/27f3caa71594e621ef22 to your computer and use it in GitHub Desktop.
Save NrI3/27f3caa71594e621ef22 to your computer and use it in GitHub Desktop.
[ Java ] Obtener un dígito de un numero entero dada una posición x
public int getPosition(int x,int n){
int c = getCantidadDigitos(n);
if (x>c || x<1) return 0;
return (n/(int)Math.pow(10,c-x))%10;
}
public int getCantidadDigitos(int n){
int c = 0;
while(n>0){
c++;
n/=10;
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment