18

How to round in java towards zero?

So -1.9 becomes -1.0 and -0.2 becomes 0.0, 3.4 becomes 3.0 and so on.

Is Math.round() capable of doing this changing some parameters?

Radek
  • 1,403
  • 3
  • 25
  • 54

8 Answers8

23

I do not believe that the standard library has such a function.

The problem is that you are asking for very different behavior (mathematically speaking) depending on whether the number is larger or smaller than 0 (i.e. rounding up for negative values, rounding down for positive values)

The following method could be used:

public double myRound(double val) {
    if (val < 0) {
        return Math.ceil(val);
    }
    return Math.floor(val);
}
Kris
  • 14,426
  • 7
  • 55
  • 65
10

cast to long like this:

float x= 1.9;

long y = (long)x;

This rounds both positive and negative numbers towards zero.

Spycho
  • 7,698
  • 3
  • 34
  • 55
confucius
  • 13,127
  • 10
  • 47
  • 66
  • 3
    Provided the result fits in an `int`, which may be a big if – NPE Dec 05 '11 at 15:11
  • 2
    Why would you cast to an `int` and store it in a `long`? Cast to a `long`. – Kevin Dec 05 '11 at 15:12
  • 2
    **-1:** this fails in cases where the floating point number is too big or too small to be represented by a 64 bit integer. Whether `int` or `long`, the problem is only shifted. Such numbers may not be common in everyday usage – but that only makes it more difficult to find bugs related to over/underflow. – TheOperator Aug 04 '16 at 12:24
7

Use RoundingMode.DOWN, it leads towards zero.

Example :

    BigDecimal value = new BigDecimal("1.4");
    value = value.setScale(0, RoundingMode.DOWN);
    System.out.println(value.doubleValue());
    BigDecimal value1 = new BigDecimal("-1.4");
    value1 = value1.setScale(0, RoundingMode.DOWN);
    System.out.println(value1.doubleValue());
mprabhat
  • 20,107
  • 7
  • 46
  • 63
5

Just casting to int will do that for you?

Edit: If you want to retain a double this should work simply enough:

if (val < 0) 
   return -Math.floor(-val);
else
   return Math.floor(val);

And just for the people who want branch free code and feel a bit more clever:

long tmp = Double.doubleToLongBits(val);
tmp >>>= 63;
return Math.floor(val) + tmp;
Voo
  • 29,040
  • 11
  • 82
  • 156
  • 3
    Provided the result fits in an `int`, which may be a big if. – NPE Dec 05 '11 at 15:10
  • 1
    @aix True enough, but then you also can't guarantee that there exists a double that can represent the number. But yes that's nitpicking on my side ;) – Voo Dec 05 '11 at 15:12
1
y=sign(x)*floor(abs(x))

or

y=sign(x)*round(abs(x)-0.5)

It should be easy to implement in Java.

The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27
benfuzius
  • 11
  • 1
0

Seems like you want to always round-down? You can use Math.floor instead

public static double floor(double a)

Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer. Special cases:

TS-
  • 4,311
  • 8
  • 40
  • 52
0

BigDecimal offers many rounding options.

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
  • In particular, that would be DOWN to solve the OP's need which is different from FLOOR in that FLOOR follows the correct mathematical behavior. – demongolem Nov 23 '15 at 00:59
0

You also can try this:

public static void main(String[] args) {
        Double myDouble = -3.2;
        System.out.println(myDouble.intValue()); //Prints -3

    }
Averroes
  • 4,168
  • 6
  • 50
  • 63