# what is (int)?

• 07-28-2013, 04:33 PM
kai
what is (int)?
hi!

int []days=new int [10];

days[i]=(int) ((interestEarn[i])/(principle[i]*rate[i]/100*1/365.0));

is there any way to make the (int) go away without changing days to double?
:(nod):
• 07-28-2013, 04:43 PM
harshit shah
Re: what is (int)?
It is called type casting mate.It converts / forces the jvm to convert data type maybe float,double to int.
Quote:

is there any way to make the (int) go away without changing days to double?
Actually the days are in int form.See the array is in int form so the days are also in int form else there would be an error mate.If your 'interestEarn' ,'principle' are in int form the you cam remove the (int)
• 07-28-2013, 04:44 PM
JosAH
Re: what is (int)?
That (int) thing is just a cast; it casts a double type value to an int type value. The expression value is of type double because you did a division by a double value (365.0)

kind regards,

Jos
• 07-28-2013, 04:52 PM
kai
Re: what is (int)?
hi jos...

so you saying just delete of the .0??

kai
• 07-28-2013, 04:56 PM
kai
Re: what is (int)?
hi harshit shah

ummm...but i have to use both interestEarn and principle as double....
coz actually it is actually much longer program =)
• 07-28-2013, 05:00 PM
harshit shah
Re: what is (int)?
Quote:

Originally Posted by kai
hi harshit shah

ummm...but i have to use both interestEarn and principle as double....
coz actually it is actually much longer program =)

if both are in double the type casting is the only way by which you can make the days in int form else the jvm would automatically convert days to double.Moreover there would a loss of precision error.
• 07-28-2013, 05:03 PM
harshit shah
Re: what is (int)?
Quote:

Originally Posted by kai
hi jos...

so you saying just delete of the .0??

kai

Even if you delete the .0 the other two 'interestEarn' ,'principle' are in double form so that would lead the days to go in point form again.
• 07-28-2013, 05:12 PM
kai
Re: what is (int)?
Quote:

Originally Posted by harshit shah
if both are in double the type casting is the only way by which you can make the days in int form else the jvm would automatically convert days to double.Moreover there would a loss of precision error.

So there is no other way.. but "have" to use the cast?
• 07-28-2013, 05:22 PM
JosAH
Re: what is (int)?
Quote:

Originally Posted by kai
So there is no other way.. but "have" to use the cast?

Yep, you have to because a double (8 bytes) doesn't fit in an int variable (4 bytes); but what is so scary about a simple cast?

kind regards,

Jos
• 07-28-2013, 05:35 PM
kai
Re: what is (int)?
Quote:

Originally Posted by JosAH
Yep, you have to because a double (8 bytes) doesn't fit in an int variable (4 bytes); but what is so scary about a simple cast?

kind regards,

Jos

haha i dont know... i heard my lecturer said there is a way to not use (int)...maybe it was not for me :(think):
• 07-29-2013, 05:48 PM
t014y
Re: what is (int)?
Quote:

Originally Posted by kai
haha i dont know... i heard my lecturer said there is a way to not use (int)...maybe it was not for me :(think):

The only problem with the type casting to int is your number will be truncated. That is to say only the the value to the left of the decimal point will remain. for example (int) 6.9 => 6.

Your teacher may have wanted you to round your answer in which case you will need to do (int) Math.round(...). But I am just speculating what your teacher wants.