java.lang.Double cannot be applied to (long) help?

I can't seem to calculate using variables with different data types.

The data types involved are double and long.

End result will be a double.

Tried converting one of the long but this error appear:

parseDouble(java.lang.String) in java.lang.Double cannot be applied to (long)

Any idea how to to calculate double and long together?

Example:

Double rate = 0.35;

Double gross = 950.0;

long totalIncome;

Double total;

total = rate * Double.parseDouble(totalIncome) + gross

That was what i did. Any ideas?

[647 byte] By [deathacka] at [2007-11-27 10:19:51]
# 1

I don't see a need to cast here. Just add the numbers together like so:

long totalIncome = 1000; // 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);

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.

Message was edited by:

petes1234

petes1234a at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...
# 2

I use the double class because my rates are in percentage.

Example: 0.57% = 0.0057

I can't use long for it, cause the compiler will give an error.

deathacka at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> I use the double class because my rates are in

> percentage.

>

> Example: 0.57% = 0.0057

>

> I can't use long for it, cause the compiler will give

> an error.

No, you don't understand. double != Double.

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.

petes1234a at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi,

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;

Regards,

Loga

Loga_07a at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...
# 5

> Regards,

> Loga

I believe you're just restating what I stated above.

petes1234a at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...
# 6

Yup, problem fixed! :) Thank you and i am so sorry for my silly mistake.

deathacka at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...
# 7

> Yup, problem fixed! :) Thank you and i am so sorry

> for my silly mistake.

Congrats! Don't apologize. This mistake had a purpose. Now you will NEVER do this again.

Good luck in your coding.

/Pete

petes1234a at 2007-7-28 17:00:10 > top of Java-index,Java Essentials,Java Programming...