Shelby Simpson

Greenhorn

Posts: 26

posted 4 years ago

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

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

posted 4 years ago

Read this and try to understand all data types thoroughly.

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

Shelby Simpson wrote:Correction - I'mnotsure 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

posted 4 years ago

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 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.

~ Mansukh

Shelby Simpson

Greenhorn

Posts: 26

posted 4 years ago

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?

Mansukhdeep Thind wrote:Shelby Simpson wrote:Correction - I'mnotsure 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

posted 4 years ago

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

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.

posted 4 years ago

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

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?

~ Mansukh

Shelby Simpson

Greenhorn

Posts: 26

posted 4 years ago

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.

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.

posted 4 years ago

Nothing to do with concatenation. You get 3 because the int type only deals with

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.

posted 4 years ago

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 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