user3009344
user3009344

Reputation:

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

Upvotes: 4

Views: 256

Answers (4)

Deekshith
Deekshith

Reputation: 1

Why do you want do declare another variable "i" .. I mean you can directly use "(int) d" where ever you want,that way you also save some memory.

Upvotes: 0

Jops
Jops

Reputation: 22705

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

Upvotes: 6

PKopachevsky
PKopachevsky

Reputation: 260

You can disable this inspection in Idea for whole project.

Main menu -> Analyze -> Configure current file analisys -> Configure inspections

Use the Seach to find "Numeric cast that loses precision" inspection and uncheck it.

Upvotes: 0

wawek
wawek

Reputation: 1597

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

Upvotes: 2

Related Questions