[Error] possible lossy conversion from double to int

미어캣의 개발일지·2023년 1월 12일

메세지

error: incompatible types: possible lossy conversion from double to int




원인

int는 정수를 저장할 때 사용, 소수점이 있는 값을 int로 변환하면 소수점 아래의 수를 잃게 된다. 따라서 double을 int로 저장하는 것은 lossy 할 수 있기 때문에 자바는 허용하지 않는다.




해결방법

  • 강제 형변환
double d = 1.2;
int n = (int)d;
profile
이게 왜 안되지? 이게 왜 되지?

0개의 댓글