Saturday, May 17, 2008

Round a double to the nearest tenth

Rounding a double to the nearest tenth in java (easy enough to change to nearest hundredth, thousandth, etc.)


int front = (int) Math.floor(originalDouble);
double end = originalDouble - front;
end *= 10;
double finalNum = front + (Math.round(end) / 10D);

No comments: