# Decimals

• 04-21-2012, 06:06 PM
Goughyy_
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.

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");               }                    }```
• 04-21-2012, 06:15 PM
Fubarable
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.
• 04-21-2012, 06:26 PM
Goughyy_
Re: Decimals
Quote:

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.:(y):
• 04-21-2012, 06:26 PM
Fubarable
Re: Decimals
You're quite welcome, and best of luck!