Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Rounding screwed up?

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Rounding screwed up?

    Ok so I have this code:

    import javax.swing.JOptionPane;
     
    public class testing{
    	public static void main (String arg[]){
    		int Answer=0;
    		String Sa="";
    		String Sb="";
    		Sa = JOptionPane.showInputDialog(null, "Enter the dividend.");
    		Sb = JOptionPane.showInputDialog(null, "Enter the divisor.");
    		int IntFormSa = Integer.parseInt(Sa);
    		int IntFormSb = Integer.parseInt(Sb);
    		Answer = IntFormSa / IntFormSb;
    		JOptionPane.showMessageDialog(null, "The quotient is "+Answer);
    		System.exit(0);
    	}
    }

    The problem is, when I enter two numbers that should divide to equal a decimal answer, it rounds to a seemingly random integer. It's not the closest integer because when I input 2 and 3 for the dividend and divisor it says that the quotient is 0 yet it should be .6 repeating. Anyone know how to fix this so that it gives the exact decimal?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Rounding screwed up?

    The primitives you are using cannot be used for floating point math. You should use a float or double, for example

    float Answer = IntFormSa / (float)IntFormS

    Note the cast in the denomenator

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Rounding screwed up?

    Ah! Thanks a lot! One more thing though: is there a way to enter a decimal as one of the numbers? When I do so, it just says that there is an error converting the string to and int because of the period I put in it.
    Last edited by The_Mexican; February 5th, 2010 at 10:47 PM. Reason: Gramatical

  4. #4
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Rounding screwed up?

    Quote Originally Posted by The_Mexican View Post
    ...it just says that there is an error converting the string to and int because of the period I put in it.
    If it has a decimal point, then it's not an int!
    You need
    float whatever = Float.parseFloat(Sa);

    Also note the usual java convention that variables are in all lower case. Conventions like this are well worth sticking to, because they make your code easier to read.

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Rounding screwed up?

    Thank you, changing it to float fixes everything! I will also remember to make my variables lowercase, so that they're easier to see. But also, why would anybody ever use int in place of float ever? float seems to fix everything.

  6. #6
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Rounding screwed up?

    Quote Originally Posted by The_Mexican View Post
    But also, why would anybody ever use int in place of float ever? float seems to fix everything.
    Lots of reasons! In fact you're probably likely to come across ints more often than floats. You just need to make sure you choose the right type of variable for what you want to do!