▷ 문제 상황 1
숫자를 올림, 내림한 결과를 알고 싶다 !
▶ 해결 방법 1
ceil( ), floor( ) 메소드를 이용
* 헤더 : #include <cmath>
int c1 = ceil(2.2); // c1 = 3
int f1 = floor(4.7); // f1 = 4
▷ 문제 상황 2
숫자를 반올림한 결과를 알고 싶다 !
▶ 해결 방법 2-1
floor( ) 메소드와 0.5 이용
* 헤더 : #include <cmath>
int f1 = floor(4.7 + 0.5); // f1 = floor(5.2) = 5
int f2 = floor(4.2 + 0.5); // f2 = floor(4.7) = 4
▶ 해결 방법 2-2
round( ) 메소드이용
* C++ 11 부터 사용 가능
* 헤더 : #include <cmath>
int c1 = round(2.2); // c1 = 2
int f1 = round(4.7); // f1 = 5
'Coding Test > C++ 줍줍' 카테고리의 다른 글
[C++ 줍줍] next_permutation과 순열/조합 (0) | 2022.09.20 |
---|---|
[C++ 줍줍] 문자열 자르기 : substr( ) (0) | 2022.09.05 |
[알튜비튜 줍줍] sort 함수 정리 (0) | 2022.09.03 |
[C++ 줍줍] 벡터를 이용한 집합 계산 : 합집합, 교집합, 차집합 (0) | 2022.07.25 |
[C++ 줍줍] string의 특정 값 제거하기 : index, 문자 기준 (0) | 2022.07.05 |