Results 1 to 4 of 4
Thread: Decimals
- 04-21-2012, 06:06 PM #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"); } }
-
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 #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Decimals
Thanks, really helped. Plus the quick reply was great. Thanks for the help. Have a nice day.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.
-
Similar Threads
-
Help with decimals
By sabarooni in forum New To JavaReplies: 4Last Post: 11-02-2011, 02:07 AM -
Need help with truncating decimals?
By akeni in forum New To JavaReplies: 3Last Post: 10-05-2011, 12:31 AM -
BIG decimals
By xael in forum New To JavaReplies: 17Last Post: 10-04-2010, 10:14 AM -
Decimals in java help!!
By Gold in forum New To JavaReplies: 3Last Post: 12-09-2009, 11:25 PM -
get more decimals?!?! please help!
By michcio in forum New To JavaReplies: 7Last Post: 05-22-2008, 10:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks