Android Float To Int

Why is it so hard to find?

public boolean onTouch(View v, MotionEvent event)

I need to convert float event.getY()to int.

Is it possible?

event.getY().intValue() won't work at all.

Any ideas?

+3
source share
4 answers

Uhhh, yes, how about:

int y = (int)event.getY();

You see that getY()only float returns for devices with subpixel precision.

+17
source

Using

Math.round(yourFloat);

better than

(int)yourFloat;

It's all about accuracy. If you use (int), you just get the numbers after the deleted point. If you use Math, you will get a rounded number. It doesn't seem like a big deal. For example, if you try to round something like 3.1, both methods will produce the same result - 3.

3,9 3,8. 4,

(int)3.9 = 3

Math.round(3.9) = 4

+2

:

int val = (int)event.getY();
0

Math.round () will round the float to the nearest integer.

0
source

Source: https://habr.com/ru/post/1749639/


All Articles