8 Replies - 4416 Views - Last Post: 21 June 2010 - 08:00 PM Rate Topic: -----

#1 Buttons225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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));


Was This Post Helpful? 0
  • +
  • -

#3 Buttons225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 Buttons225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,656
  • 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
   inputs[i] = scan.nextInt(); //assign input to the appropriate element
}



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

Was This Post Helpful? 0
  • +
  • -

#6 mrsuchacz  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#7 Buttons225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 Buttons225  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,656
  • Joined: 27-December 08

Re: Too many decimal places

Posted 21 June 2010 - 08:00 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1