wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes double.0 trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double.0 trouble" Watch "double.0 trouble" New topic
Author

double.0 trouble

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
I am having issues with a method that is not giving me the result I thought it would and I think it has something to do with the double data type. The method is -


My volume is 1728, WEIGHT_PER_CM3 is 2. I get 3.456 but the method returns 3. I'm sure I understand the double data type and think it might have something to do with it.

thanks
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Correction - I'm not sure I understand the double data type
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Shelby Simpson wrote:Correction - I'm not sure I understand the double data type


Read this and try to understand all data types thoroughly.

What is the return type of getVolume() method? Is it int?


~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Shelby Simpson wrote:I am having issues with a method that is not giving me the result I thought it would and I think it has something to do with the double data type. The method is -


My volume is 1728, WEIGHT_PER_CM3 is 2. I get 3.456 but the method returns 3. I'm sure I understand the double data type and think it might have something to do with it.

thanks


You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Mansukhdeep Thind wrote:
Shelby Simpson wrote:Correction - I'm not sure I understand the double data type


Read this and try to understand all data types thoroughly.

What is the return type of getVolume() method? Is it int?


That was it, the return type was int. Does int concatenation work the same as string. So to say that everything int + becomes a int? So now that volume is a double everything following will also be a double?
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Mansukhdeep Thind wrote:

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.


I confused, the decimal was a result of dividing whole numbers.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Shelby Simpson wrote:
Mansukhdeep Thind wrote:

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.


I confused, the decimal was a result of dividing whole numbers.


What are you confused about? Can you show me the whole code?
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Mansukhdeep Thind wrote:
Shelby Simpson wrote:
Mansukhdeep Thind wrote:

You get 3.456 because you have been taught in 2nd or 3rd grade how to go about multiplying decimal numbers. But you have to tell the computer to do that for you. It does not understand what is going on in your mind.


I confused, the decimal was a result of dividing whole numbers.


What are you confused about? Can you show me the whole code?



I thought the issue was with concatenation. I thought it was converting my 3.456 to an int. I just don't understand how multiplying decimals plays into this.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

That was it, the return type was int. Does int concatenation work the same as string.

Nothing to do with concatenation. You get 3 because the int type only deals with whole numbers so something like 3.4 gets rounded down to 3. All integer arithmetic rounds down so 3.8 would also yield the result 3.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18522
    
  40

Shelby Simpson wrote:I thought the issue was with concatenation. I thought it was converting my 3.456 to an int. I just don't understand how multiplying decimals plays into this.


An int multiply with an int results in an int. An int divided by an int results in an int. And in your example, an int multiply with an int and then divided by an int results in an int. Your method doesn't know that you need a double until it is time to return the value

Java didn't convert "3.456" into an int. It converted your int result into a double -- and when it does that, it is highly unlikely that doubles which came from ints turn into something that isn't a whole number.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Oh ok, I understand now. Thanks for the help everyone. I appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double.0 trouble
 
Similar Threads
Generic class question
retrieval of different types - typedef?
Array reflection in Java
Referring to an array-HELP!
I keep getting this error and dont know what to do to fix it?