3 Replies - 668 Views - Last Post: 05 October 2012 - 10:47 AM Rate Topic: -----

#1 mon_e_mons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 05-October 12

How would I print only two 3 decimal places here?

Posted 05 October 2012 - 10:02 AM

Hey there. I'm making a program to convert metric to imperial and I'm wondering how I would make the output have the number only extend 3 digits after the decimal point. I was thinking a substring but I'm not totally sure how to implement it here. In this bit of code the program reads what's in the metric side's textfields (meters, etc.) and puts the conversion into the imperial side's textfields. Since this is where it prints you an answer I felt this was the suitable piece to show you. If that's not the case, inform me and I'll give more. Keep in mind that the numbers BEFORE the decimal could be any size. Could be 5 numbers, could be just one. who knows. But I want to format it so the output shows the entire number before the decimal and 3 digits after the decimal. Any help would be appreciated.
double A,B,C,D;
				Object ButtonPressed = Punch.getSource();

				JConversionTables You = new JConversionTables();

				if(ButtonPressed == MetricToEnglish)
				{
					A = Double.parseDouble(TCentimeters.getText());
					B = Double.parseDouble(TMeters     .getText());
					C = Double.parseDouble(TKilograms  .getText());
					D = Double.parseDouble(TCelsius    .getText());

					TInches   .setText("" + You.CentimetersToInches(A));
					TFeet     .setText("" + You.MetersToFeet       (B)/>);
					TPounds   .setText("" + You.KilogramsToPounds  (C));
					TFarenheit.setText("" + You.CelsiusToFarenheit (D));
				}
				if(ButtonPressed == EnglishToMetric)
				{
					A = Double.parseDouble(TInches   .getText());
					B = Double.parseDouble(TFeet     .getText());
					C = Double.parseDouble(TPounds   .getText());
					D = Double.parseDouble(TFarenheit.getText());

					TCentimeters.setText("" + You.InchesToCentimeters(A));
					TMeters     .setText("" + You.FeetToMeters       (B)/>);
					TKilograms  .setText("" + You.PoundsToKilograms  (C));
					TCelsius    .setText("" + You.FarenheitToCelsius (D));
				}


Is This A Good Question/Topic? 0
  • +

Replies To: How would I print only two 3 decimal places here?

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: How would I print only two 3 decimal places here?

Posted 05 October 2012 - 10:16 AM

Check out this reference:

Formatting Numeric Print Output

This post has been edited by rfs02: 05 October 2012 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 mon_e_mons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 05-October 12

Re: How would I print only two 3 decimal places here?

Posted 05 October 2012 - 10:44 AM

View Postrfs02, on 05 October 2012 - 10:16 AM, said:

Check out this reference:

Formatting Numeric Print Output



Hmm...well I know about the printf command but I'm not sure how to use it here because the display is put into a textfield right away and displayed there instead of in a print. Is it possible in this piece of code to have it getText from the textfields, then somehow do the computation (which is in "You". As in You is my instance of JConversionTables which is where all of the actual math goes on) and format it. Once it's formatted it would be placed in the textfield?
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: How would I print only two 3 decimal places here?

Posted 05 October 2012 - 10:47 AM

You could always use a NumberFormatter.

Using Number Formatters
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1