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: Variable Division Issues

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

    Default Variable Division Issues

    I am trying to write code to monitor percentage progress of file transfers by dividing a running total by the total number of bytes in the file.

    For some reason, when I divide the total by the total file bytes, I continuously get zero as a result.

    total = long
    percent = long
    fileBytes = long

    		int bytesRead;
    		while((bytesRead = in.read(bytes))>0) {
    			total+=bytesRead;
    			percent = (long) ((total/fileBytes)*100);
    			System.out.println(Double.toString(percent));
    			buffout.write(bytes,0,bytesRead);
    		}
    		buffout.close();

    I can't figure out why this is happening. Is the large number of file bytes outside of Java's range for longs?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Variable Division Issues

    In integer division: 4/5 = 0 & 5/3 = 1
    There is no fractional part. Results are rounded to whole number.
    Use float or double to get fractional parts.

Similar Threads

  1. [SOLVED] Performing Division with Double Variables
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 05:36 PM
  2. Recursion issues.
    By ender16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2011, 09:03 PM
  3. division problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 30th, 2010, 01:51 PM
  4. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  5. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM