Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double.0 trouble

 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction - I'm not sure I understand the double data type
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok, I understand now. Thanks for the help everyone. I appreciate it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic