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 4 of 4

Thread: Rounding with printf

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rounding with printf

    Hello, I made a program and it works great, but I need the height to be rounded to the nearest 0.1

    Here is the program:
    import java.util.Scanner;
     
    public class BouncingBall
    {
      public static void main(String[] args)
      {
        Scanner keyboard = new Scanner(System.in);
        int bounce = 0, time = 0;
        double height = 0.0;
        double velocity = 0;
     
        System.out.println("Enter the initial velocity of the ball: ");
        velocity = keyboard.nextDouble();
     
        do
        {
          System.out.println("Time: " + time + " Height: " + height);
          time ++;
     
          height += velocity;
          velocity -= 32;
     
          if (height < 0)
          {
            height *= -0.5;
            velocity *= -0.5;
            bounce++;
     
            System.out.println("Bounce!");
          }
        } while (bounce < 5);
      }
    }

    I tried System.out.printf("Time: " + time + " Height: " + height); instead of println, but the output looks all funny. However, all the numbers are rounded properly. How do I make it so the output doesn't get all messed up?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rounding with printf

    What do you mean by "looks all funny" and "get all messed up"
    If you search printf you should find plenty of samples as well as documentation to show how to use it.
    If you run into a problem you can not solve show the code with the printf statement and explain what the problem is

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rounding with printf

    Sorry I wasn't more specific.

    Here's how the output looks when I use println:
    Time: 0 Height: 0.0
    Time: 1 Height: 100.0
    Time: 2 Height: 168.0
    Time: 3 Height: 204.0
    Time: 4 Height: 208.0
    ...
    Here's how the output looks when I use printf:
    Time: 0 Height: 0.0Time: 1 Height: 100.0Time: 2 Height: 168.0Time: 3 Height: 204.0Time: 4 Height: 208.0 ...
    Each time and height is outputted onto the same line (which I don't want), but the height values are rounded to the tenth value in later values (which is what I do want).

  4. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Rounding with printf

    Do you know what the ln stands for in println?
    Look for an equivalent in Formatter API

    Look under conversions header in that doc. It may not be clear straight away, but you will want to escape that character.

Similar Threads

  1. Printf syntax woes
    By ineedahero in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2013, 09:14 AM
  2. Java printf
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 27th, 2013, 03:20 PM
  3. [SOLVED] printf error
    By Thor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 10:05 PM
  4. How to align text? Printf?
    By shifat96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 20th, 2012, 12:51 PM
  5. about printf please help having errors
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 11:01 PM