# Thread: Convert an objects data from int to double

1. Member
Join Date
Mar 2011
Posts
4
Rep Power
0

## 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.:(

2. 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. Moderator
Join Date
Apr 2009
Posts
13,130
Rep Power
23
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. Member
Join Date
Mar 2011
Posts
4
Rep Power
0
Thanks both of you!D
It fixed my value outputs

#### Posting Permissions

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