Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By JosAH

Thread: Doubt in TYPE CASTING

  1. #1
    meena22228 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Question Doubt in TYPE CASTING

    Hi All,


    Can anyone clarify my doubt in JAVA?

    Check my below code

    import java.io.*;
    public class roundin
    {

    public static void main(String args[])
    {
    double amount=138.26;
    double tmpAmount =amount*1000;
    int tempam=(int)tmpAmount;
    System.out.println(tempam);
    }
    }

    output = 138260

    When I try by giving value for amount as 118.26 or 138.26 or whatever i get similar outputs.
    My doubt is when i try with the value 128.26 alone i am getting the output as 128259. Please any of you clarify me why i am getting so. Please clarify as soon as possible.
    Last edited by meena22228; 07-26-2012 at 01:42 PM.

  2. #2
    vhonanivb is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Doubt in TYPE CASTING

    Hi.

    I am failing to understand your problem.

    but any I hope you understand that by using code below
    Java Code:
    int tempam=(int)tmpAmount;
    you are truncating the result you got after multiplying, that meaning in the int only numbers before the commas(.) will be stored not rounded off.

    Please advise further if you still not clear.

  3. #3
    meena22228 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Doubt in TYPE CASTING

    Thanks for ur reply but my doubt is when i try giving value as 138.26 or 118.26 i get output as 138260 or 118260 respectively. I get different output only if i give the value as 128.26 answer for this i get is 128259. I want to know the reason for this

  4. #4
    meena22228 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Doubt in TYPE CASTING

    To be more clear of my doubt if give 128.26 also i shld get as 128260 only rite but i am not getting as 128260 instead i am getting as 128259. This is my doubt. Hope now you are clear of my doubt

  5. #5
    vhonanivb is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Doubt in TYPE CASTING

    Ok

    In your code , just after:
    Java Code:
    double tmpAmount =amount*1000;
    Put an output statement so that you may be able to see the value before it is truncated.

    Like I said in my last post:
    Java Code:
    int tempam=(int)tmpAmount;
    that code does not round-off the number, it just truncates the numbers after (.) ... you are expecting to see a rounded value of 128259.99999999999 which is incorrect.

    Only the numbers before (.) are stored in the int.

    Hope I am making sense this time.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Doubt in TYPE CASTING

    Decimal numbers often do not map to a floating point binary exactly, in the same way that 1/3 does not map exactly as a decimal number.
    This is what's happening here.
    128.26 is 128.25999999999999 (etc etc) as a floating point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default Re: Doubt in TYPE CASTING

    Considering the above reason rounding might help

    int tempam=(int)Math.round(tmpAmount);
    Swastik

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Doubt in TYPE CASTING

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Doubt in TYPE CASTING

    Quote Originally Posted by dswastik View Post
    Considering the above reason rounding might help

    int tempam=(int)Math.round(tmpAmount);
    I would suggest not using doubles if they need the accuracy.
    To be honest, without knowing exactly what it is they're doing there may well be ways to completely avoid this calculation.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    meena22228 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Doubt in TYPE CASTING

    Quote Originally Posted by vhonanivb View Post
    Ok

    In your code , just after:
    Java Code:
    double tmpAmount =amount*1000;
    Put an output statement so that you may be able to see the value before it is truncated.

    Like I said in my last post:
    Java Code:
    int tempam=(int)tmpAmount;
    that code does not round-off the number, it just truncates the numbers after (.) ... you are expecting to see a rounded value of 128259.99999999999 which is incorrect.

    Only the numbers before (.) are stored in the int.

    Hope I am making sense this time.
    Thanks for the reply... I did all those already i have an other doubt If we multiply 128.26 by 1000 we must get 128260 only rite while printing i am getting as 12825.9999999.how is it possible.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Doubt in TYPE CASTING

    Did you read my post?
    I explained why in that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Doubt in TYPE CASTING

    In threads like this one should throw this link in and run away.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Doubt in TYPE CASTING

    I would, but I never have it to hand and can never be bothered to Google it...:)
    Besides my "you can't represent 1/3 as a decimal" usually works...clearly not in this case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Posting Permissions

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