# Too many decimal places

Page 1 of 1

## 8 Replies - 8032 Views - Last Post: 21 June 2010 - 08:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=178606&amp;s=b012e21cb13c61c1711f53efd7fdc62a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Buttons225

Reputation: 0
• Posts: 14
• Joined: 14-June 10

# Too many decimal places

Posted 21 June 2010 - 06:40 PM

Hello everyone

I am in a Java programming online class so my professor is 0 help. I would appreciate any input anyone can give.

I am writing a program for standard deviation which I have down just fine, however the numbers my program outputs have 16 places beyond the decimal point. ( 3.1100000000000003) How can I shorten this down to just 2 places beyond the decimal point (3.11)

I will embed my code if it will help:

```import java.util.Scanner;

public class Test3 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter first number: ");
double first = input.nextDouble();
System.out.print("Enter second number: ");
double second = input.nextDouble();
System.out.print("Enter third number: ");
double third = input.nextDouble();
System.out.print("Enter fourth number: ");
double fourth = input.nextDouble();
System.out.print("Enter fifth number: ");
double fifth = input.nextDouble();
System.out.print("Enter sixth number: ");
double sixth = input.nextDouble();
System.out.print("Enter seventh number: ");
double seventh = input.nextDouble();
System.out.print("Enter eighth number: ");
double eighth = input.nextDouble();
System.out.print("Enter ninth number: ");
double ninth = input.nextDouble();
System.out.print("Enter tenth number: ");
double tenth = input.nextDouble();

double mean = ((first + second + third + fourth + fifth + sixth + seventh + eighth + ninth + tenth)/ 10);
double variance = ((10 - mean) * (10 - mean))/10;
double deviation = Math.sqrt(variance);

System.out.println("The mean is: " + mean);
System.out.println("The standard deviation is: " + deviation);
}
}
```

Thanks so much!!

Is This A Good Question/Topic? 0

## Replies To: Too many decimal places

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Too many decimal places

Posted 21 June 2010 - 06:42 PM

One way: DecimalFormat

```DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(deviation));

```

### #3 Buttons225

Reputation: 0
• Posts: 14
• Joined: 14-June 10

## Re: Too many decimal places

Posted 21 June 2010 - 06:51 PM

Do I need to import a java.util to make that work?

### #4 Buttons225

Reputation: 0
• Posts: 14
• Joined: 14-June 10

## Re: Too many decimal places

Posted 21 June 2010 - 07:04 PM

Ohh man, I figured it out. This Java Programming sure is testy. Thanks so much!

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Too many decimal places

Posted 21 June 2010 - 07:11 PM

Also, you'll probably want to use arrays here rather than a variable for each number. This way, you can automate with loops, and you don't have to prompt manually for each number, or get the average manually as you have done. So for example:
```int[] inputs = new int[10]; //ten int variables initialized to 0
for(int i = 0; i < inputs.length; i++){ //go from elem 0 through length-1
System.out.println("Enter number " + (i+1)); //prompt for input
}

```

You might also want to check out TriggaMike's arrays tutorial in the Java Tutorials section.

This post has been edited by macosxnerd101: 21 June 2010 - 07:12 PM

### #6 mrsuchacz

Reputation: 4
• Posts: 31
• Joined: 15-May 10

## Re: Too many decimal places

Posted 21 June 2010 - 07:22 PM

another way to do this if you're just wanting to print it out is use printf
```System.out.printf("The mean is: %.2f", mean);

```

where .2 between % and f determines how many decimal places will be shown

### #7 Buttons225

Reputation: 0
• Posts: 14
• Joined: 14-June 10

## Re: Too many decimal places

Posted 21 June 2010 - 07:51 PM

You were right, macosxnerd101, using array is much nicer. How do I add the inputs together to get the mean though?

Here is what I have:
```import java.text.DecimalFormat;

public class Project06Problem02 {

public static void main(String[] args) {
final int NUMBERS = 10;
double[] numbers = new double[NUMBERS];

java.util.Scanner input = new java.util.Scanner(System.in);

int[] inputs = new int[10];
for(int i = 0; i < NUMBERS; i++){
System.out.println("Enter number " + (i+1));
numbers[i] = input.nextDouble();
}

double mean = (inputs.length/ 10);
double sum = Math.pow((10 - mean), 2);
double variance = sum/9;
double deviation = Math.sqrt(variance);

DecimalFormat df = new DecimalFormat("0.00");
System.out.println("The mean is: " + df.format(mean));
System.out.println("The standard deviation is: " + df.format(deviation));
}
}

```

This post has been edited by Buttons225: 21 June 2010 - 07:52 PM

### #8 Buttons225

Reputation: 0
• Posts: 14
• Joined: 14-June 10

## Re: Too many decimal places

Posted 21 June 2010 - 07:57 PM

I guess I should have more faith in myself. I did figure this out. Thank you so very much everyone!

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Too many decimal places

Posted 21 June 2010 - 08:00 PM