Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing Precision on purpose

While converting double to int:

double d = 5.5;
int i = (int) d;

Variable "i" becomes 5. That's exactly what i want, the problem is i am getting the warning: Cast to 'int' from 'double' may result in loss of precision My question is, is there any way to tell IDE that such precision loss is expected so i will not get the warning? So is there any other way to lose precision and cast to integer?

I know that i could disable all loss precision warnings at all, but it wouldn't be nice. I am using Intellij IDEA 13


2 Answers

Consider adding @SuppressWarnings("NumericCastThatLosesPrecision") on your method. This would be a portable solution to your concern.

like image 120
Jops Avatar answered Sep 04 '25 05:09

Jops


The best thing is to use Double and Integer. Then you can use intValue() method.

like image 22
wawek Avatar answered Sep 04 '25 05:09

wawek