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: How do I get my answers to out with 2 decimal places?

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

    Default How do I get my answers to out with 2 decimal places?

    Hi I have my program working all I need now is to output the answers to 2 decimal places. Could somebody help me out with this? Below is my code.




    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package assignment2;

    import java.text.DecimalFormat;
    import java.util.Scanner;

    /**
    *
    * @author Adam
    */
    public class PowerSquare {

    public static void main(String[] args) {


    double num1;
    double num2;
    double num1a;
    double num2a;


    Scanner scan = new Scanner(System.in);

    System.out.println("Enter 2 numbers");
    num1 = scan.nextDouble();



    for (double i = num1; i <= num1 {
    num1a =(i * i * i);



    num2 = scan.nextDouble();
    num2 = (num2 < 0 ? -num2 : num2);

    for (double a = num2; a <= num2{
    num2a =(Math.sqrt(a));
    num2a =(Math.round(num2a));


    if (num1a > num2a){

    System.out.print(num1a);
    }
    else if (num2a > num1a){

    System.out.print(num2a);
    }
    else if (num2 < 0){

    DecimalFormat fmt = new DecimalFormat("#.##");
    System.out.println(num2a);
    }
    System.exit(0);
    }







    }}}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How do I get my answers to out with 2 decimal places?

    The API is your best friend. Check out the DecimalFormat class.

    Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: How do I get my answers to out with 2 decimal places?

    I still don't really understand? I have been trying for hours and just can't seem to get it right.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I get my answers to out with 2 decimal places?

    One of the aspects of programming newcomers need to learn is to use all resources at your disposal, and this doesn't exclude doing web searches. A quick search turns up lots of useful hits, including the following:
    http://www.javaprogrammingforums.com...al-places.html
    If you still do not understand, make an effort to write some code, use the API posted above, and post back exactly what about you don't understand.

  5. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (February 8th, 2012)

Similar Threads

  1. New to Java Need 2 decimal places, please :). Here is my code
    By Charyl in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2011, 03:06 AM
  2. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  3. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  4. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM