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: How to round the numbers?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    GB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to round the numbers?

    Hello,

    I'm new on this forum, I'm from Belgium, this is my first year IT and I'm 19 years old.
    I want that you help me with my problems about programming.

    The first problem is that the numbers don't round in IntelliJ.
    Example: (sorry, it's in Dutch)
    Geef de vorige kilometerstand: 125
    Geef de huidige kilometerstand: 900
    Geef het aantal getankte liters: 50
    Verbruik voor 775km: 6.451612903225806/100km

    That 6.4516..... is the problem, how can I make it 6.45/100km?

    If you want a picture of the program that I wrote, just ask it.
    thanks!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to round the numbers?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    You could use the printf() method to format the output to the desired precision. The printf() method and the formatting needed to get the desired output can be a bit confusing at first, so give it a try and come back if you need help.

    If help is needed, please post the code you need help with correctly using code or highlight tags per the above link.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    GB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to round the numbers?

    This is the code, can you see what's wrong?
    But if you want, I can translate it in English.

     public class Verbruik {
        public static void main(String[] args) {
            int vorige, huidige;
            float aantalLiter, aantal, resultaat;
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.print("Geef de vorige kilometerstand: ");
            vorige = keyboard.nextInt();
            System.out.print("Geef de huidige kilometerstand: ");
            huidige = keyboard.nextInt();
            System.out.print("Geef het aantal getankte liters: ");
            aantal = keyboard.nextFloat();
     
            resultaat = huidige - vorige; // =aantal liters / resultaat * 100 //
            aantalLiter = (( aantal / resultaat )*100);
            System.out.printf("Verbruik voor " + resultaat + "km" + ": " + aantalLiter + " liter" + "/100km");
        }
    }
    Last edited by Javabeginner19; September 27th, 2014 at 07:27 AM.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to round the numbers?

    Yes I can. Did you review the API for the printf() method? Or you might try this Oracle tutorial.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Location
    GB
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to round the numbers?

    No I don't know what it is, can you just say what's wrong in the code?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to round the numbers?

    Telling you what's wrong and suggesting how to fix it is not as helpful to you as showing you how to figure it out yourself, so that's not my first choice. Don't give up on yourself yet. I'm not.

    What did you get from the tutorial page I suggested? That page gives specific examples you can use to make your printf() method more effective and do what you'd like. Try what is suggested there and come back with your changes.

Similar Threads

  1. Help me to round off double, PLEASE JAVA MASTERS!!!
    By iamprogrammer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 8th, 2014, 02:12 AM
  2. Replies: 3
    Last Post: September 7th, 2013, 10:35 AM
  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, 05: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, 03:35 PM