import java.lang.*;
class Ideone {
System.
out.
println(PegaPrimirosDigitos
(12345678,
4)); System.
out.
println(PegaPrimirosDigitos
(-12345678,
4)); System.
out.
println(PegaPrimirosDigitos
(123,
4)); System.
out.
println(PegaPrimirosDigitos
(-12,
4)); System.
out.
println(PegaPrimirosDigitos
(0,
0)); }
public static int PegaPrimirosDigitos(int valor, int digitos) {
digitos
= Math.
max(1, digitos
); int positivo
= Math.
abs(valor
); int tamanho
= (int)(Math.
log10(positivo
) + 1); if(digitos > tamanho) {
return valor;
}
return valor
/ (int)Math.
pow(10, tamanho
- digitos
); }
}
aW1wb3J0IGphdmEubGFuZy4qOwoKY2xhc3MgSWRlb25lIHsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbiB7CgkJU3lzdGVtLm91dC5wcmludGxuKFBlZ2FQcmltaXJvc0RpZ2l0b3MoMTIzNDU2NzgsIDQpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4oUGVnYVByaW1pcm9zRGlnaXRvcygtMTIzNDU2NzgsIDQpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4oUGVnYVByaW1pcm9zRGlnaXRvcygxMjMsIDQpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4oUGVnYVByaW1pcm9zRGlnaXRvcygtMTIsIDQpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4oUGVnYVByaW1pcm9zRGlnaXRvcygwLCAwKSk7Cgl9CglwdWJsaWMgc3RhdGljIGludCBQZWdhUHJpbWlyb3NEaWdpdG9zKGludCB2YWxvciwgaW50IGRpZ2l0b3MpIHsKCQlkaWdpdG9zID0gTWF0aC5tYXgoMSwgZGlnaXRvcyk7CgkJaW50IHBvc2l0aXZvID0gTWF0aC5hYnModmFsb3IpOwoJCWludCB0YW1hbmhvID0gKGludCkoTWF0aC5sb2cxMChwb3NpdGl2bykgKyAxKTsKICAgICAgICBpZihkaWdpdG9zID4gdGFtYW5obykgewogICAgICAgICAgICByZXR1cm4gdmFsb3I7CiAgICAgICAgfQoJCXJldHVybiB2YWxvciAvIChpbnQpTWF0aC5wb3coMTAsIHRhbWFuaG8gLSBkaWdpdG9zKTsKCX0KfQ==