What is the rounding principle?

double y = 1.5; int x = (int) y; 

    2 answers 2

    The whole part remains, all the rest is discarded (rounded to the smaller). Even if y = 1.999999, then x will still be 1

      The fractional part is discarded, i.e. 1.5 rounded to 1 , and -1.5 rounded to -1 . If the number does not fit into the int range, then it is rounded to the nearest range border:

        100000000000d -> Integer.MAX_VALUE Double.POSITIVE_INFINITY -> Integer.MAX_VALUE -100000000000d -> Integer.MIN_VALUE Double.NEGATIVE_INFINITY -> Integer.MIN_VALUE 

      A separate story with NaN - it is reduced to 0 .