Results 1 to 11 of 11

Thread: what is (int)?

  1. #1
    kai
    kai is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

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

  2. #2
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: what is (int)?

    It is called type casting mate.It converts / forces the jvm to convert data type maybe float,double to int.
    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)
    Last edited by harshit shah; 07-28-2013 at 04:46 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kai
    kai is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Re: what is (int)?

    hi jos...

    so you saying just delete of the .0??


    kai

  5. #5
    kai
    kai is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default 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 =)

  6. #6
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: what is (int)?

    Quote Originally Posted by kai View Post
    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.

  7. #7
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: what is (int)?

    Quote Originally Posted by kai View Post
    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.

  8. #8
    kai
    kai is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Re: what is (int)?

    Quote Originally Posted by harshit shah View Post
    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?

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default Re: what is (int)?

    Quote Originally Posted by kai View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    kai
    kai is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Re: what is (int)?

    Quote Originally Posted by JosAH View Post
    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

  11. #11
    t014y is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: what is (int)?

    Quote Originally Posted by kai View Post
    haha i dont know... i heard my lecturer said there is a way to not use (int)...maybe it was not for me
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •