-5

help, I want the output of the program to be rounded to 2 decimal places..............................................................................................................................................................................

here is my code :

package javaapplication2;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NewClass {
    public static void main(String[ ] args){

int age = 0;             
age = Integer.parseInt(
JOptionPane.showInputDialog(
null, "Enter radius of a circle: "));  

        int radius = 0;
        System.out.println("Enter radius of a circle : ");
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            radius = Integer.parseInt(br.readLine());
        }
        catch(NumberFormatException ne)
        {
            System.out.println("Invalid radius value" );
            System.exit(0);
        }
        catch(IOException ioe)
        {
            System.out.println("IO Error :" + ioe);
            System.exit(0);
        }
        radius = 5;
        double circumference = 2 * Math.PI * radius;
        double area = Math.PI * radius * radius;

        System.out.println("Area of a circle is " + area);
        System.out.println("Perimeter of a circle is " + circumference);

        }
}  
  • 1
    This is not a "here's my code, fix it for me" type site. Please isolate your problem and ask a decent question. – Hovercraft Full Of Eels Nov 30 '12 at 02:00
  • see http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – BZapper Nov 30 '12 at 02:01
  • my question is : help, I want the output of the program to be rounded to 2 decimal places above """here is my code :"""" – Chris Valenzuela Nov 30 '12 at 02:03
  • You've got a ton of code unrelated to the problem. All you need to do is post 5 lines of code that summarize the issue and then ask a specific question regarding *that code*. – Hovercraft Full Of Eels Nov 30 '12 at 02:06
  • Please read, [Writing the Perfect Question](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) for more on how to include the useful information and exclude the chaff so that we'll have an easier time understanding your actual problem. – Hovercraft Full Of Eels Nov 30 '12 at 02:09
  • Please don't discourage the site's new visitors with downvotes. Constructive Criticism...Downvoting some one will discourage our friend to follow these advices. – Naeem Ul Wahhab Nov 30 '12 at 02:17

2 Answers2

1

As you only want to display rounded values, rather than pass it on for use in further calculations, you could use DecimalFormat:

DecimalFormat format = new DecimalFormat("0.##");
System.out.println("Area of a circle is " + format.format(area));
System.out.println("Perimeter of a circle is " + format.format(circumference));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • @ChrisValenzuela Friend Here's an upvote from me. Don't discourage by those who are always just downvoting others – Naeem Ul Wahhab Nov 30 '12 at 02:24
  • -TheNoble-Coder thanks sir, I'm a student and new to programming and to this site.. I'm glad I can still see people with positive attitudes ^_^ – Chris Valenzuela Nov 30 '12 at 02:30
  • @ChrisValenzuela You are welcome. I remember when people did that to me when I was new here. We all are students just here to learn and help :-) – Naeem Ul Wahhab Nov 30 '12 at 02:40
1

You can apply the following:

public double Round2(double number) {
       return Math.rint(number*100)/100;
}
Gutenberg
  • 992
  • 1
  • 7
  • 17