8 Replies - 2818 Views - Last Post: 03 October 2011 - 09:22 AM Rate Topic: -----

#1 nathan_williamson94   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-March 11

Rounding numbers to three decimal places

Posted 03 October 2011 - 08:09 AM

For my Computer Science class I have to make a program that will print out the area, circumference, and radius of a circle using the radius that was given to me in the assignment. I have almost everything done but I was asked in the directions to round the output to three decimal places and I do not know how to do this. I have tried several different things but since it was last week when i was working on this I do not remember what I did exactly. I tried things that I found on random pages that came up when I tried to search for an answer using Google but none of them worked. Below is my code for this program and I will be very grateful for any help!


/**
 * Program should print the radius, area, and the circumference of a circle.
 * 
 * Nathan WIlliamson
 * Week 4
 * Pro54c - Circles
 * Sep 29, 2011
 * 
 */
public class Circles
{
    // Varible for Pi and Radius that will be used for calculations
    double pi     = 3.14159d;
    double radius = 3.712d;

    public Circles()
    {
        // Calculations for area, and circumference
        Area          = (pi *radius);
        Circumference = ((2 * pi) * radius);
        
        //rounding numbers

        
        //printout radius, area, and circumference
        System.out.println("The Radius of the circle = " + radius);
        System.out.println("The Area of the circle = " + Area);
        System.out.println("The Circumference of the circle = " + Circumference);
    }
}



Sorry this is not the correct code...in this one I dont have the variables right. I post post the code in the next two mins.


/**
 * Program should print the radius, area, and the circumference of a circle.
 * 
 * Nathan WIlliamson
 * Week 4
 * Pro54c - Circles
 * Sep 29, 2011
 * 
 */
public class Circles
{
    // Varible for Pi and Radius that will be used for calculations and varibles 
    // for output
    double pi     = 3.14159d;
    double radius = 3.712d;
    double circumference, area;
    
    
    public Circles()
    {
        // Calculations for area, and circumference
        area          = (pi *radius);
        circumference = ((2 * pi) * radius);
        
        //rounding numbers

        
        //printout radius, area, and circumference
        System.out.println("The Radius of the circle = " + radius);
        System.out.println("The Area of the circle = " + area);
        System.out.println("The Circumference of the circle = " + circumference);
    }
}



ok this is my current code!

Is This A Good Question/Topic? 0
  • +

Replies To: Rounding numbers to three decimal places

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 08:10 AM

Take a look at the DecimalFormat class.

This post has been edited by Ryano121: 03 October 2011 - 08:34 AM

Was This Post Helpful? 3
  • +
  • -

#3 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 08:11 AM

You can use printf() functionality to format your output to number of decimal places. FOr example:
double x = 3.444478;
System.out.printf("%.3f",x);//this will make a three decimal place output


Also you can think of String.format() method of class String.
Was This Post Helpful? 2
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1281
  • View blog
  • Posts: 4,089
  • Joined: 12-May 09

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 08:33 AM

quadruple post!
Was This Post Helpful? 1
  • +
  • -

#5 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 08:41 AM

View Postxclite, on 03 October 2011 - 04:33 PM, said:

quadruple post!


Quintuple now :P (yeah I had to look that up)
Was This Post Helpful? 0
  • +
  • -

#6 nathan_williamson94   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-March 11

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 09:15 AM

Thanks for the help. Everything run great now. Although, I do have one question... The part of code that I changed is below, why did I have to add a blank line for the output to be on three different lines?

System.out.printf("The Radius of the circle = " + "%.3f",area);
        System.out.println();
        System.out.printf("The Area of the circle = " + "%.3f",area);
        System.out.println();
        System.out.printf("The Circumference of the circle = " + "%.3f",circumference);



the output without the inserted blank lines should up as printed below

The Radius of the circle = 11.662The Area of the circle = 11.662The Circumference of the circle = 23.323

But shouldn't it show up like this...(the way it does now)

The Radius of the circle = 11.662
The Area of the circle = 11.662
The Circumference of the circle = 23.323
Was This Post Helpful? 0
  • +
  • -

#7 nathan_williamson94   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-March 11

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 09:21 AM

Thanks for the help. Everything run great now. Although, I do have one question... The part of code that I changed is below, why did I have to add a blank line for the output to be on three different lines?

System.out.printf("The Radius of the circle = " + "%.3f",area);
        System.out.println();
        System.out.printf("The Area of the circle = " + "%.3f",area);
        System.out.println();
        System.out.printf("The Circumference of the circle = " + "%.3f",circumference);



the output without the inserted blank lines should up as printed below

The Radius of the circle = 11.662The Area of the circle = 11.662The Circumference of the circle = 23.323

But shouldn't it show up like this...(the way it does now)

The Radius of the circle = 11.662
The Area of the circle = 11.662
The Circumference of the circle = 23.323

And the reason for smohd's quad post is probably the same reason for my double post. DreamInCode is having a few problems today. I am not sure why but there are several possibilities such as maintenance or too many users at once.
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 09:21 AM

Because the .printf is not like the .println method in that it doesn't put a line break at the end of the statement.

In .printf however you can use the escape sequence \n to accomplish the same thing.

e.g -

System.out.printf("The Radius of the circle = " + "%.3f\n",area);
System.out.printf("The Area of the circle = " + "%.3f\n",area);
System.out.printf("The Circumference of the circle = " + "%.3f",circumference);

Was This Post Helpful? 2
  • +
  • -

#9 aj_lavaca   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 18-February 11

Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 09:22 AM

View Postnathan_williamson94, on 03 October 2011 - 09:16 AM, said:

Thanks for the help. Everything run great now. Although, I do have one question... The part of code that I changed is below, why did I have to add a blank line for the output to be on three different lines?


Try googling "Java Escape Sequences" and you'll find what your looking for. You'll b able to take out at least 2 lines.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1