7 Replies Latest reply: Jul 12, 2007 1:28 AM by 807605 RSS

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

    807605
      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?
        • 1. Re: java.lang.Double cannot be applied to (long) help?
          807605
          I don't see a need to cast here. Just add the numbers together like so:
                   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);
          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
          • 2. Re: java.lang.Double cannot be applied to (long) help?
            807605
            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.
            • 3. Re: java.lang.Double cannot be applied to (long) help?
              807605
              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.
              • 4. Re: java.lang.Double cannot be applied to (long) help?
                807605
                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
                • 5. Re: java.lang.Double cannot be applied to (long) help?
                  807605
                  Regards,
                  Loga
                  I believe you're just restating what I stated above.
                  • 6. Re: java.lang.Double cannot be applied to (long) help?
                    807605
                    Yup, problem fixed! :) Thank you and i am so sorry for my silly mistake.
                    • 7. Re: java.lang.Double cannot be applied to (long) help?
                      807605
                      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