# How would I print only two 3 decimal places here?

Page 1 of 1

## 3 Replies - 992 Views - Last Post: 05 October 2012 - 10:47 AMRate 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=294415&amp;s=10b142a59e745f31dc6d1299c31f77f7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mon_e_mons

Reputation: 0
• Posts: 34
• 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

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

### #3 mon_e_mons

Reputation: 0
• Posts: 34
• Joined: 05-October 12

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

Posted 05 October 2012 - 10:44 AM

rfs02, 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?

### #4 Kinaces

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