티스토리 뷰
language/java
[에러] incompatible types: possible lossy conversion from double to int
kirinman 2022. 11. 10. 20:51int는 정수를 저장할 때 사용되기 때문에 소수점이 있는 실수형을 int로 변환하면 소수점 아래의 값을 잃기 때문에 이런 에러가 난다.
ex) 21.75를 int로 변환한다고 하면 0.75부분을 잃기 때문에 자바에서는 lossy이기에 이를 허용하지 않는다.
-해결방법
형변환을 하자!
요런 식으로
public class Main {
public static void main(String[] args) {
int price = 120000;
double finalPrice = 0;
if (price < 100000) {
finalPrice = price;
} else if (price < 300000) {
finalPrice = (int) (price * 0.95);
} else if (price < 500000) {
finalPrice = (int) (price * 0.9);
} else {
finalPrice = (int) (price * 0.8);
}
System.out.println(finalPrice);
}
}
'language > java' 카테고리의 다른 글
Error: Gradle script '~~~' is not executable (0) | 2023.01.31 |
---|---|
자바 chars()이용해서 문자열의 특정 값 개수 가져오기 (0) | 2023.01.17 |
substring 문자열 자르기 (0) | 2023.01.07 |
221214 자바 기초 과제 (0) | 2022.12.14 |
[java] if문에서 break와 return의 차이? (0) | 2022.11.22 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- jmeter시나리오
- 항해
- jmeter세션
- jmeter쿠키
- Python
- EC2
- 대규모더미데이터
- Redis
- jmeter테스트
- jwt
- 동적크롤링
- 자바
- 토큰
- jmeter부하테스트
- CorrectnessAndTheLoopInvariant
- index
- Redisson
- hackerrank
- 인덱스
- Lock
- Spring
- jmeter토큰
- bankersRounding
- Java
- 부하테스트시나리오
- CheckedException
- pessimisticlock
- 스프링faker
- jmeter로그인
- 프로그래머스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함