Get first digits of a number in Java

0

How do I get part of an integer in Java? For example, I have a int a = 12345678 variable, and the user wants to receive only 4 digits.

    
asked by anonymous 04.03.2015 / 23:55

2 answers

6

There are several ways you can do this. One of these is to transform string with valueOf() and get a piece of it with the substr() " and then convert back to int , like this:

import java.lang.*;

class Ideone {
    public static void main (String[] args) throws java.lang.Exception {
        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);
        String texto = String.valueOf(positivo);
        if(digitos > texto.length()) {
            return valor;
        }
        return Integer.parseInt(texto.substring(0, digitos)) * Integer.signum(valor);
    }
}

See running on ideone .

It can also be done mathematically without involving conversions but will really you do not know how to do this?

import java.lang.*;

class Ideone {
    public static void main (String[] args) throws java.lang.Exception {
        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);
    }
}

See working on ideone .

I do not guarantee that it will work in all situations, but the point of the solution is there. For lack of a definition of criterion I chose better that at least it should take 1 digit. I also understood that the negative sign should be considered but it is not a digit.

This problem seems to be artificial and does not serve anything real but the solution is there. If you have more details, I might be able to give you a better solution. The solution here is already better than the one defined in the question.

    
05.03.2015 / 00:16
4

At first, you can convert the number to a String and then use the substring method to "cut" from index 0 to the numbers of digits that should be returned. For example:

public int foo(int number, int digits){
   String str = Integer.toString(number);
   if(digits> str.length())
       return number;

    boolean isNegative = str.startsWith("-"); 
    return Integer.valueOf(str.substring(0, isNegative ? digits + 1 : digits));
}

Example running on Ideone

    
05.03.2015 / 00:15