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 2 of 2

Thread: Processing Decimals <1 & >0

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Processing Decimals <1 & >0

    I have a feeling this is something fundamental I missed... but how can I get an operation such as 1/2 to return 0.5?

    Right now all I get is 0.0. Example:

    public class NumberProblem {
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		double result = 1/2;
    		System.out.println(result);
    	}
    }
    Output = 0.0

    Is there a formatting operation that has to be done?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Processing Decimals <1 & >0

    The compiler treats 1/2 as integer division. Change one to a double to get floating division: 1.0/2

  3. The Following User Says Thank You to Norm For This Useful Post:

    bgroenks96 (July 24th, 2011)

Similar Threads

  1. Processing Arrays
    By av8 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 27th, 2011, 06:19 AM
  2. Image Processing
    By subash in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 8th, 2011, 08:40 AM
  3. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM
  4. Convert Decimals to Octals using only If/Else
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 9th, 2011, 12:58 PM
  5. Signal processing API.
    By ivanloes in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 05:56 PM