Results 1 to 4 of 4

Thread: Decimals

  1. #1
    Goughyy_ is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    Goughyy_ is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default 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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help with decimals
    By sabarooni in forum New To Java
    Replies: 4
    Last Post: 11-02-2011, 03:07 AM
  2. Need help with truncating decimals?
    By akeni in forum New To Java
    Replies: 3
    Last Post: 10-05-2011, 01:31 AM
  3. BIG decimals
    By xael in forum New To Java
    Replies: 17
    Last Post: 10-04-2010, 11:14 AM
  4. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 12:25 AM
  5. get more decimals?!?! please help!
    By michcio in forum New To Java
    Replies: 7
    Last Post: 05-22-2008, 11:26 PM

Posting Permissions

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