Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help me to round off double, PLEASE JAVA MASTERS!!!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me to round off double, PLEASE JAVA MASTERS!!!

    SOURCE CODE:

    Double a, b, c;

    a = Double.parseDouble(txt1.getText());
    b = Double.parseDouble(txt2.getText());
    c = a + b;
    txt3.setText(""+c);
    }


    SAMPLE OUTPUT:

    1.234 + 1.23 = 2.464


    PROBLEM:

    How to round off that sum 2.464 as 2.46?
    Will I put ".2f"?


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    898
    Thanks
    0
    Thanked 166 Times in 148 Posts

    Default Re: Help me to round off double, PLEASE JAVA MASTERS!!!

    You could multiply by 100, turn it to an integer and cast it back to double dividing by 100.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me to round off double, PLEASE JAVA MASTERS!!!

    Good day sir!
    Sir, I'm just a 17 year old Beginner Java Programmer, aiming to be become an expert someday.
    Could you retype the code for me sir? Please Java Master!

  4. #4
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Help me to round off double, PLEASE JAVA MASTERS!!!

    What Cornix said is absolutely correct. I am no expert in Java. I just started java 4 or 5 months ago & i really liked this language. I dont know what you are trying to do with your code but casting is something i know.
    I would like to give you a simple example or casting & you could try this and adjust according to your code:

    double temp = 2.464;
    float res = (int)(temp*100); //this (int) does the casting and removes the last digit as you want
    res = res/100;
    System.out.println(res);

    Hope this helped you a little bit!

    --- Update ---

    & with your code i think this should work

    Double a, b, c;

    a = Double.parseDouble(txt1.getText());
    b = Double.parseDouble(txt2.getText());
    c = a + b;
    float temp = (int)(c*100);
    temp = temp/100;
    text3.setText(""+temp);
    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Help me to round off double, PLEASE JAVA MASTERS!!!

    Sir, I'm just a 17 year old Beginner Java Programmer, aiming to be become an expert someday.
    Could you retype the code for me sir? Please Java Master!
    Perfect, but without continual practice writing code your attempts to become an expert get that much harder.


    How to round off that sum 2.464 as 2.46?
    Do you wish to round, or floor? Because casting - as suggested above - will do the latter not the former.

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me to round off double, PLEASE JAVA MASTERS!!!

    Use Printf Statement for formatting text

Similar Threads

  1. PLEASE HELP ME JAVA MASTERS!!!
    By iamprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 4th, 2014, 04:18 PM
  2. [SOLVED] How to round to 2 decimal places in a dialog box
    By JohnEliot in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2013, 07:39 PM
  3. [SOLVED] How to make Math.round() round to the nearest .1?
    By bdennin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 2nd, 2013, 07:23 PM
  4. Newbie all round
    By camillejb in forum Member Introductions
    Replies: 1
    Last Post: June 25th, 2012, 06:08 PM
  5. take a double, then round decimal to either to .0 or .5 only
    By MaxBodine in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2011, 04:35 PM