I need to convert Object o to Double. Is the correct way to convert it to String first?
-
2What is the special type of your Object ? Is it actually a Double, or something else ?nos– nos2011-09-21 17:19:09 +00:00Commented Sep 21, 2011 at 17:19
-
What is Object? Is it an unknown class or a Double class that has been typed to an Object.Rontologist– Rontologist2011-09-21 17:19:45 +00:00Commented Sep 21, 2011 at 17:19
-
"I try to found, but all seems vague." - your question is too vague to get an obvious answer. please elaborate. What is 'Object o' and what's the intention?Saket– Saket2011-09-21 17:23:01 +00:00Commented Sep 21, 2011 at 17:23
8 Answers
new Double(object.toString());
But it seems weird to me that you're going from an Object to a Double. You should have a better idea what class of object you're starting with before attempting a conversion. You might have a bit of a code quality problem there.
Note that this is a conversion, not casting.
2 Comments
Double(String) and Double(double) constructors were deprecated in JDK 9 and deprecated for removal in Java 16If your Object represents a number, eg, such as an Integer, you can cast it to a Number then call the doubleValue() method.
Double asDouble(Object o) {
Double val = null;
if (o instanceof Number) {
val = ((Number) o).doubleValue();
}
return val;
}
1 Comment
You can't cast an object to a Double if the object is not a Double.
Check out the API.
particularly note
valueOf(double d);
and
valueOf(String s);
Those methods give you a way of getting a Double instance from a String or double primitive. (Also not the constructors; read the documentation to see how they work) The object you are trying to convert naturally has to give you something that can be transformed into a double.
Finally, keep in mind that Double instances are immutable -- once created you can't change them.
Comments
You can use the instanceof operator to test to see if it is a double prior to casting. You can then safely cast it to a double. In addition you can test it against other known types (e.g. Integer) and then coerce them into a double manually if desired.
Double d = null;
if (obj instanceof Double) {
d = (Double) obj;
}
Comments
In Java version prior to 1.7 you cannot cast object to primitive type
double d = (double) obj;
You can cast an Object to a Double just fine
Double d = (Double) obj;
Beware, it can throw a ClassCastException if your object isn't a Double
2 Comments
java.lang.Number.There's no equivalent for primitive types.
Also worth mentioning -- if you were forced to use an older Java version prior to 1.5, and you are trying to use Collections, you won't be able to parameterize the collection with a type such as Double.
You'll have to manually "box" to the class Double when adding new items, and "unbox" to the primitive double by parsing and casting, doing something like this:
LinkedList lameOldList = new LinkedList();
lameOldList.add( new Double(1.2) );
lameOldList.add( new Double(3.4) );
lameOldList.add( new Double(5.6) );
double total = 0.0;
for (int i = 0, len = lameOldList.size(); i < len; i++) {
total += Double.valueOf( (Double)lameOldList.get(i) );
}
The old-school list will contain only type Object and so has to be cast to Double.
Also, you won't be able to iterate through the list with an enhanced-for-loop in early Java versions -- only with a for-loop.
2 Comments
Tried all these methods for conversion ->
public static void main(String[] args) {
Object myObj = 10.101;
System.out.println("Cast to Double: "+((Double)myObj)+10.99); //concates
Double d1 = new Double(myObj.toString());
System.out.println("new Object String - Cast to Double: "+(d1+10.99)); //works
double d3 = (double) myObj;
System.out.println("new Object - Cast to Double: "+(d3+10.99)); //works
double d4 = Double.valueOf((Double)myObj);
System.out.println("Double.valueOf(): "+(d4+10.99)); //works
double d5 = ((Number) myObj).doubleValue();
System.out.println("Cast to Number and call doubleValue(): "+(d5+10.99)); //works
double d2= Double.parseDouble((String) myObj);
System.out.println("Cast to String to cast to Double: "+(d2+10)); //works
}
