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

• February 8th, 2012, 10:17 AM
dunnage888
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);
}

}}}
• February 8th, 2012, 10:29 AM
KevinWorkman
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
• February 8th, 2012, 12:16 PM
dunnage888
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.
• February 8th, 2012, 12:23 PM
copeg
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.