You can use Double.parseDouble() to convert your string to double. This will work even if the string is an integer:
String myString = "1234.56";
double myDouble = Double.parseDouble(myString);
If you need to check if it is an integer or a double:
String myString = "1234.56";
int myInt;
double myDouble = Double.parseDouble(myString);
if ((myDouble % 1) == 0) {
myInt = (int) myDouble;
System.out.println("myString is an integer: " + myInt );
} else {
System.out.println("myString is a double: " + myDouble );
}
Or you can use Guava's DoubleMath.isMathematicalInteger():
String myString = "1234.56";
int myInt;
double myDouble = Double.parseDouble(myString);
if (DoubleMath.isMathematicalInteger(myDouble)) {
myInt = (int) myDouble;
System.out.println("myString is an integer: " + myInt );
} else {
System.out.println("myString is a double: " + myDouble );
}
Double.parseDouble(). This will work for all of the above examples.Numberclass ?