Results 1 to 4 of 4
  1. #1
    snozzla is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Convert an objects data from int to double


    I have 3 integers in my Length class, mm, cm & m.
    These values are input by user into 3 objects of the Length class.
    Now I want to convert these values into double values instead of integers, then add the 3 objects together.

    This is what I have tried:

    public double to_double(Length other) {
    temp = m + cm/100 + mm/1000;
    return temp;

    and when I print it to screen I get values like: 2.0, 1.0, 1.0
    Which isn't what the input is.

    How do you go about doing this? I really don't know what to do as of now.:(

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power


    I'm not sure what your problem is but I'll gaze into my crystal ball.

    Java Code:
    double value = (double) (9 / 2);
    The variable will have the value 4.0 and not 4.5. This is because integer division is performed before the cast to double. Therefore 9 divided by 2 is 4 and that gets cast to 4.0. If you are not getting the correct answers then I assume you are doing something similar. Make sure you cast at least one value to a double before doing any calculations.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    In your case, since you are dividing by fixed values, simply make those fixed values 100.0 and 1000.0.
    That should do the trick.

  4. #4
    snozzla is offline Member
    Join Date
    Mar 2011
    Rep Power

Similar Threads

  1. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 05:27 PM
  2. How to convert a double into a int?
    By tyang in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 10:02 AM
  3. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 02:46 AM
  4. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts