# Rounding numbers to three decimal places

Page 1 of 1

## 8 Replies - 2879 Views - Last Post: 03 October 2011 - 09:22 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249644&amp;s=074ba94a736d67a5e8795a202377147f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nathan_williamson94

Reputation: 1
• 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;

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

//rounding numbers

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 circumference, area;

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

//rounding numbers

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

• D.I.C Lover

Reputation: 1461
• 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

### #3 smohd

• Critical Section

Reputation: 1824
• 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.

### #4 xclite

• I wrote you an code

Reputation: 1282
• Posts: 4,091
• Joined: 12-May 09

## Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 08:33 AM

### #5 Ryano121

• D.I.C Lover

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

## Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 08:41 AM

xclite, on 03 October 2011 - 04:33 PM, said:

Quintuple now (yeah I had to look that up)

### #6 nathan_williamson94

Reputation: 1
• 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

### #7 nathan_williamson94

Reputation: 1
• 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.

### #8 Ryano121

• D.I.C Lover

Reputation: 1461
• 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);
```

### #9 aj_lavaca

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

## Re: Rounding numbers to three decimal places

Posted 03 October 2011 - 09:22 AM

nathan_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.