Separate Double values in their Entire Part & Decimal

1

you want to separate a Double value for example Double entrada = 34.567 and process it so that the result is:

Parte entera: 34

Parte decimal: 567

but that the value after the decimal point leaves without the 0. (number) simply the number as if it were whole

    
asked by Mauri Brito 06.12.2018 в 03:39
source

2 answers

5

With the following algorithm you can do that. As we know, a String is practically an array of characters, or array of Char . Therefore we can split the chain, or obtain several substrings from this chain.

With this in mind, we can use this knowledge to split a floating number into its integer and decimal. What the substring method does is to obtain a substring from a position in the original string, and with the indexOf method we obtain that number, the position where a character is found within the string.

With these 2 simple functions we can obtain the whole and decimal part of any floating number.

double dato = 34.567;

String cad = dato + "";

String entero = cad.substring(0, cad.indexOf("."));
String decimal = cad.substring(cad.indexOf(".") + 1);

System.out.println(cad);
System.out.println("Parte Entera: " + entero);
System.out.println("Parte Decimal: " + decimal);
    
answered by 06.12.2018 в 09:15
0

HELLO THE WAY I WOULD DO IT WOULD BE LIKE THAT .... EVEN THERE IS ANOTHER WAY.

public class Probando {

    public static void main(String[] args) {
        double dato = 19.3456;        //Primero creas una variable en el main con tu valor.
        System.out.println(sacarNumeroDespuesDelPunto(dato)); // y la ingresas en este método.
        // La respuesta sería 3456.
    }

    /*
     * Se muy bien lo que necesitas.
     * Te explicare.. te hice este metodo.. lo puedes...
     */
public static long sacarNumeroDespuesDelPunto(double valor){ // creamos un método que reciba un double y retorne un long.

    long respuesta = 0; // Inicializamos la variable a retornar.
    String dato = ""+ valor;  //convertimos el double en un STRING para poder trabajar con el.
    char signo= '.'; // Creamos un char con signo punto(.) que tiene un double.
    int coincidencia = dato.indexOf(signo); // Utilizamos un entero.. Aqui tomamos a dato y le ponemos un 
    // indexOf() <-- Lo que hace el.. busca la primera coincidencia en caracteres...
    // Entonces le mandamos a signo..  y nos dice en que posisción se encuentra el punto.
    // ejemplo 23.657  La posisción sería 2 porque empieza a contar desde 0 como un arreglo.
    //Recuerda que si en indexOf no encuentra nada COINCIDENCIA valdría -1.

    if(coincidencia != -1){ // Ponemos esta condicción por si coincidencia diferente a -1, osea que encontro un punto en el número
    String parte = dato.substring(coincidencia+1); // utilizamos un subString para cortar la parte. le decimos
    // Corte a dato despues del punto y deme todo lo que haya despues.. si quisieras que solo te den solo los dos decimales le
    //Pones tope ejemplo. String parte = dato.substring(coincidencia+1, 3 ); Recuerda que cuenta desde el 0.

    respuesta =Long.parseLong(parte); //por ultimo conviertes a parte en un long.
    }

    return respuesta; //Retornas la respuesta.


    }

}

I hope you can use the method.

    
answered by 06.12.2018 в 08:39