0

I cannot find any pattern rounding off doubles in an array. I am trying to make a program that will input a double like grades,money etc., and round it off in a whole number.

  • I don't know Java but I find this for you - http://stackoverflow.com/a/153753/1542290 so ain't sure how you didn't find any patterns – Mr. Alien Oct 08 '14 at 12:06
  • 3
    1) itterate trough array and round your number, 2) dont use doubles for money – user902383 Oct 08 '14 at 12:06

2 Answers2

0

There are no short cut available, You need to iterate through the array and use Math#floor or Math#ceil or Math.round to round off.

Math#ceil - Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

Math#floor - Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

Math#round - Returns the closest int to the argument, with ties rounding up

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

You could do it like this:

private int[] roundOff(double[] doubleArray) {

    int[] integerArray = new int[doubleArray.length];

    for (int i = 0; i < doubleArray.length; i++) {

       integerArray[i] = (int) Math.round(doubleArray[i]);

    }

    return integerArray;
}
Krayo
  • 2,492
  • 4
  • 27
  • 45
Moddaman
  • 2,538
  • 3
  • 23
  • 41