1. Member
Join Date
Apr 2012
Posts
3
Rep Power
0

## Decimals

I'm trying to make a Litres to Cubic Mitres Java Program. For the job I'm currently working on. I've programmed it the way I was taught but can't seem to get the conversion result to 5 decimal places. Here is the program itself it does work and complies no errors are found.

Java Code:
```import java.text.NumberFormat;
import javax.swing.JOptionPane;

public class LitreMeters
{
public static void main(String[] args)
{

String numString1;
String numString2;

JOptionPane.showMessageDialog(null,
"The conversion is 1 Litre = 0.001 Cubic Metres\n" + "Click Ok to start converting.");

numString1 = JOptionPane.showInputDialog(
"Enter amount of litres:");

//Edward Gough Programming

int userNum = Integer.parseInt(numString1);

int otherNum = 20;

int cNum = 1000;

int result = userNum / 1000;

JOptionPane.showMessageDialog(null,
userNum + " Litres converted to " + result + " Cubic Meters");

}

}```

2. ## Re: Decimals

Do you mean that you want to display a floating point result, something like 23.1222? If so, consider making result a double variable, not an int, and obtaining it by dividing userNum by a double literal as well, by 1000.0 rather than 1000. You may find that you have too many decimal places showing, and if so may have to format your results with printf or String.format(...), or a DecimalFormat object.

3. Member
Join Date
Apr 2012
Posts
3
Rep Power
0

## Re: Decimals

Do you mean that you want to display a floating point result, something like 23.1222? If so, consider making result a double variable, not an int, and obtaining it by dividing userNum by a double literal as well, by 1000.0 rather than 1000. You may find that you have too many decimal places showing, and if so may have to format your results with printf or String.format(...), or a DecimalFormat object.
Thanks, really helped. Plus the quick reply was great. Thanks for the help. Have a nice day.

4. ## Re: Decimals

You're quite welcome, and best of luck!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•