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);