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

Thread: How do I change my output to 1 decimal place?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do I change my output to 1 decimal place?

    I need a bit of help setting one of my programmes outputs to only the 1 decimal place each time the doubleResult displays it's result it is to like 12 decimal places or something like that. How do I set it so that it only displays as 3.2? Below is my programme code.


    import java.util.Scanner;

    public class Casting {


    public static void main(String[] orcs) {

    double number1 = 0;
    double number2 = 0;
    int integerResult;
    double doubleResult;
    Scanner scan = new Scanner(System.in);


    System.out.print("Enter number 1: ");
    number1 = scan.nextDouble();

    int i1 = (int) number1;
    int i2 = (int) number2;

    System.out.print("Enter number 2: ");
    number2 = scan.nextDouble();



    System.out.println("integerResult:" + i2 / i1);
    System.out.println("doubleResult" + number2 / number1);
    }
    }


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    DecimalFormat df = new DecimalFormat("#.#");
    df.format(value[0]);

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    Sorry to be a pain but where about in the code do I put it or does that not matter?

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    import java.util.Scanner;
     
    public class Casting {
     
     
    public static void main(String[] orcs) {
     
    DecimalFormat df = new DecimalFormat("#.#");
    double number1 = 0;
    double number2 = 0;
    int integerResult;
    double doubleResult;
    Scanner scan = new Scanner(System.in);
     
     
    System.out.print("Enter number 1: ");
    number1 = scan.nextDouble();
     
    int i1 = (int) number1;
    int i2 = (int) number2;
     
    System.out.print("Enter number 2: ");
    number2 = scan.nextDouble();
     
     
     
    System.out.println("integerResult: " + i2 / i1);
     
    double finalValue = number2 / number 1;
    System.out.println("doubleResult: " + df.format(finalValue));
    }
    }

    Just copy and paste this to replace your code..

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    Everything is fine from that piece of code you gave me apart from one line of code.....

    double finalValue = number2 / number 1;

    There is an error here and the programme wont run.

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    That's because when you initialize a double its got to be 0.0 not 0....

    double number1 = 0;
    double number2 = 0;
     
    CORRECT:
     
    double number1 = 0.0;
    double number2 = 0.0;

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    Yeah I tried doing that but it still has a problem with that line of code. I don't get why?

  8. #8
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    What's the problem exactly please?

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    The line of code redlines and displays the message

    ';' expected

    cannot find symbol
    symbol: variable number
    location: class assignment1.Casting

  10. #10
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    oh wait..

    double finalValue = number2 / number1;

    Sorry no space is to be put inbetween number & 1 .....

    replace old line with this 1 ive posted..

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    Ahhh thank you that works! However, my integerResult is now displaying 0 and no answer.

  12. #12
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    Try this:

    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    public class Casting {
     
     
    public static void main(String[] orcs) {
     
    DecimalFormat df = new DecimalFormat("#.#");
    double number1 = 0;
    double number2 = 0;
    int integerResult;
    double doubleResult;
    Scanner scan = new Scanner(System.in);
     
     
    System.out.print("Enter number 1: ");
    number1 = scan.nextDouble();
     
    System.out.print("Enter number 2: ");
    number2 = scan.nextDouble();
     
    int i1 = (int) number1;
    int i2 = (int) number2;
     
     
    System.out.println("integerResult: " + i1 / i2);
     
    double finalValue = number2 / number1;
    System.out.println("doubleResult: " + df.format(finalValue));
    }
    }

    You were trying to print out in an opposite direction.

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    Nope that does the same as before hmmm. Just incase this helps when I enter number 1 and number 2 I input the following:

    Enter number 1: 5.2
    Enter number 2: 16.8093

    Then this is the output displayed:

    integerResult: 0
    doubleResult: 3.2

    The integerResult needs to be 3. This work has to be submitted in 40 minutes too. Just can't get my head around this.

  14. #14
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    public class Casting {
     
     
    public static void main(String[] orcs) {
     
    DecimalFormat df = new DecimalFormat("#.#");
    DecimalFormat df2 = new DecimalFormat("#");
     
    double number1 = 0;
    double number2 = 0;
    int integerResult;
    double doubleResult;
    Scanner scan = new Scanner(System.in);
     
     
    System.out.print("Enter number 1: ");
    number1 = scan.nextDouble();
    double number4 = number1;
     
    System.out.print("Enter number 2: ");
    number2 = scan.nextDouble();
    double number3 = number2; 
     
    df2.format(number3);
    df2.format(number4);
    double resulti = number3 / number4;
     
    System.out.println("integerResult: " + df2.format(resulti));
     
    double finalValue = number2 / number1;
    System.out.println("doubleResult: " + df.format(finalValue));
    }
    }

    try that.. my results were:
    Enter number 1: 5.2
    Enter number 2: 16.6
    integerResult: 3
    doubleResult: 3.2

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do I change my output to 1 decimal place?

    Yess! It works thank you so much!

  16. #16
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    press the thanks button under my post =P will feel much at home watching it add up hehe.. Plus this is 1 problem i beat kevin to :$

  17. The Following User Says Thank You to macko For This Useful Post:

    dunnage888 (November 3rd, 2011)

  18. #17
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I change my output to 1 decimal place?

    Thanks mate Have fun at school

Similar Threads

  1. A place to call home
    By Tsarin in forum Member Introductions
    Replies: 1
    Last Post: October 14th, 2011, 08:56 AM
  2. I cannot get the right output in the right place. Where is my problem?
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 08:28 AM
  3. Where to place my own library?
    By hexwind in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 07:25 AM
  4. great place to start
    By flotsam in forum The Cafe
    Replies: 2
    Last Post: April 22nd, 2010, 11:52 AM
  5. Best way to learn java for beginners
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: May 8th, 2008, 05:37 AM

Tags for this Thread