Results 1 to 3 of 3

Thread: Quick Java Division fix

  1. #1
    Member Spardan's Avatar
    Join Date
    Feb 2003
    Location
    Drunk and Disordered
    Posts
    221

    Quick Java Division fix

    Hi guys...

    I'm doing static task allocation - need to divide a task into iterations.

    I have a long, which is the total iterations, and want to divide by 3 for three nodes.

    Problem is, how to round up/down to make sure I only get whole numbers?

    Was thinking something like this:

    long totalIterations = <a number>;

    long chunk = totalIterations/3;

    long chunk1 = chunk.roundUp();
    long chunk2 = chunk.roundUp();
    long chunk3 = totalIterations - (chunk1+chunk2);

    So does anyone know of a method for rounding up (or down) a long with decimal places?

    Thanks!
    ++Spardan++

  2. #2
    Member Spardan's Avatar
    Join Date
    Feb 2003
    Location
    Drunk and Disordered
    Posts
    221
    Goddamn I'm dumb...

    Math.round, perhaps?

    meh.


    I'd been looking in the java.math package... it never occurred to me (ever!) that there was a Math object...

    ah well... I live (barely) and learn (eventually)!
    ++Spardan++

  3. #3
    Ultimate Member DocEvi1's Avatar
    Join Date
    Dec 2001
    Posts
    2,330
    rounding is easily done using

    Code:
    return new Integer(long+0.5).intValue();
    . Thats all the Math.round function does (logic been an int value just cuts the decimals off therefore 0.2+0.5 = 0, but 0.7+0.5 = 1).
    Stefan

Posting Permissions

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