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

# Thread: How do I get my answers to out with 2 decimal places?

1. ## 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;

/**
*
*/
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. ## 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

3. ## 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. ## 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)