Results 1 to 2 of 2
  1. #1
    yo1mcool is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default Need help rounding. =/

    This is my current code.
    Java Code:
    import javax.swing.JOptionPane;
    public class TwoDecimalPlaces
    	public static void main (String[] args)
    		String outputStr;
    		String inputStr;
    		float x;
    		inputStr = JOptionPane.showInputDialog
    				("Enter the number you would like rounded to two decimal places");
    		x = Float.parseFloat(inputStr);
    		outputStr = "Rounded to two Decimal Places: " + x + "\n";
    		JOptionPane.showMessageDialog(null, outputStr,
    			"Rounding Numbers:", OptionPane.INFORMATION_MESSAGE);
    How can I make it so the output is rounded to two decimal places?

    I tried google searching with no luck... and I am still kind of new so the API is rather confusing to me still, lol.

    Thanks for any help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You don't want to round the number, you want to format the String that displays the number. Look at using either DecimalFormat, a subclass of NumberFormat or String.format(...).

Similar Threads

  1. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 01:04 AM
  2. Help with java Rounding
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:25 AM

Posting Permissions

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