# Convert an objects data from int to double

• 03-01-2011, 11:30 PM
snozzla
Convert an objects data from int to double
Hi!

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.:(
• 03-01-2011, 11:45 PM
Junky
I'm not sure what your problem is but I'll gaze into my crystal ball.

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.
• 03-02-2011, 10:42 AM
Tolls
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.
• 03-02-2011, 01:06 PM
snozzla
Thanks both of you!:o:D
It fixed my value outputs