Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Junky

Thread: Rounding to one decimal place

  1. #1
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Rounding to one decimal place

    Hello,

    Im using Bluej :

    public double hitsGround()
    {
    double currentHeight = 0;
    double time = 0;

    do
    {
    currentHeight = initialHeight + velocity * time - 16 * time * time;
    time = time + 0.1;

    } while (currentHeight >= 0);

    return time;

    }
    }

    I want the code to return the 'time' as one decimal place.
    I don't know what to do.

    Thanks for the help.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Rounding to one decimal place

    You will need to reference the decimal formatting class for this. You can find the information you need here: DecimalFormat (Java 2 Platform SE v1.4.2)
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Rounding to one decimal place

    Thank you but the code looks a little confusing

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Rounding to one decimal place

    Here is an example that I put together:

    Java Code:
    import java.text.DecimalFormat;
    
    public class test 
    {
    	public static void main(String[] args) 
    	{
    		double number = 25.5069;
    		DecimalFormat df = new DecimalFormat(".#");
    		
    		System.out.println("Number before: " + number);
    		System.out.print("Number after: " + df.format(number));
    	}
    }
    First you will to import the Decimal Format class (shown at the top). Then inside your code you will need to create a Decimal Format variable. This will be used to changed the output of your double to one decimal place e.g. ".#" in the code. Hope this helps a little more!
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Rounding to one decimal place

    So instead of 25.5069, i would have to use my 'time' variable?

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Rounding to one decimal place

    If you are returning time to one decimal place, yes, you would do something like... return df.format(time). You're on the right track!
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Rounding to one decimal place

    Important to remember is that the DecimalFormat class does not change the value of the variable. It simply returns a String representation of the value. That means if you have a variable with the value 1.234 DecimalFormat will give 1.2 but 1.234 will still be stored in the variable.
    joshdgreen likes this.

  8. #8
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Rounding to one decimal place

    So this a whole new method right?

    Is not inside my hitsGround() method?

  9. #9
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Rounding to one decimal place

    Quote Originally Posted by threlot View Post
    So this a whole new method right?

    Is not inside my hitsGround() method?
    You can put it wherever you want it. Inside your hitsGround() method would work...
    Sincerely, Joshua Green
    Please REP if I help :)

  10. #10
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

Similar Threads

  1. One Decimal Place
    By Akirien in forum New To Java
    Replies: 1
    Last Post: 08-21-2012, 11:16 AM
  2. Rounding Decimal Places
    By neverbend in forum JCreator
    Replies: 4
    Last Post: 09-29-2011, 11:41 PM
  3. Rounding decimal error
    By NixasMuraki in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 01:07 AM
  4. Compile Error (decimal rounding attempt)
    By mylosol in forum New To Java
    Replies: 2
    Last Post: 05-08-2011, 11:13 PM
  5. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM

Posting Permissions

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