• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

double.0 trouble

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correction - I'm not sure I understand the double data type
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok, I understand now. Thanks for the help everyone. I appreciate it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic