This content has been marked as final. Show 7 replies
I don't see a need to cast here. Just add the numbers together like so:
A question though: is there a reason that you are using the Double class rather than the double primative? Also, you don't jinitialize totalIncome in your code example. I assume that this is just an oversite.
long totalIncome = 1000L; // there should be the letter "L" on the end here // the forum software is deleting it double grossIncome = 950.0; double total = totalIncome + grossIncome; System.out.println(total);
Message was edited by:
I use the double class because my rates are inNo, you don't understand. double != Double.
Example: 0.57% = 0.0057
I can't use long for it, cause the compiler will give
double is a primative type, the one you should probably be using unless you need a true object to say insert into a list.
Double is a class that holds a double primative value, but also has other Object properties. It is overkill in your situation.
Also, read the API before posting here. If you did, you'd see that Double.parseDouble() does not "cast" anything. Instead it takes a string parameter (never a long or any other primative type) and converts a String to double.
What kind of error you are getting ?. Basically you can able to do operations with out any conversions from long to double. Anyhow double is greater than long, we can directly caluclate (instead of using casting). This is called as widening conversion. But if you want to calculate a double and long and the result should be assigned to long means, that time you need explicit casting (this is called as narrow casting).
Please have a look into the sample code snippet.
Double rate = 0.35; Double gross = 950.0; long totalIncome = 0l; Double total; total = rate * totalIncome + gross;