int < - > float
int intValue = 10;
float floatValue = static_cast<float>(intValue);
float floatValue = 9.99f;
int intValue = static_cast<int>(floatValue);
int < - > double
int intValue = 10;
double doubleValue = static_cast<double>(intValue);
double doubleValue = 9.99;
int intValue = static_cast<int>(doubleValue);
int를 부동소수점으로 변환 시 반올림
- 반올림 (Round): std::round 함수를 사용.
이 함수는 헤더에 정의되어 있고, std::round는 가장 가까운 정수로 반올림한다.
#include <cmath>
double value = 9.5;
int roundedValue = static_cast<int>(std::round(value));
반 내림
- 반내림 (Floor): std::floor 함수를 사용.
이 함수도 헤더에 정의되어 있으며, 항상 낮은 쪽으로 가장 가까운 정수로 내린다.
#include <cmath>
double value = 9.9;
int flooredValue = static_cast<int>(std::floor(value));
반 올림 다른 버전
- 반올림 (Ceil): std::ceil 함수 사용.
이 함수는 항상 높은 쪽으로 가장 가까운 정수로 올린다.
#include <cmath>
double value = 9.1;
int ceiledValue = static_cast<int>(std::ceil(value));