17

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,347
  • 3
  • 24
  • 52

8 Answers8

21

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
  • 13,960
  • 7
  • 50
  • 64
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,190
  • 3
  • 32
  • 54
confucius
  • 12,529
  • 10
  • 45
  • 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
6

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
  • 19,229
  • 7
  • 42
  • 62
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
  • 26,852
  • 9
  • 70
  • 145
  • 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.

dorukayhan
  • 1,435
  • 4
  • 20
  • 26
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-
  • 3,973
  • 8
  • 37
  • 51
0

BigDecimal offers many rounding options.

tobiasbayer
  • 9,921
  • 4
  • 42
  • 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
  • 3,928
  • 6
  • 43
  • 61