C에서 가장 가까운 작은 int로 두 배를 반올림하는 방법?
문제
두 배가 있습니다.
double d = 25.342;
어떻게 변환 할 수 있습니까? 25
값?
만약 그렇다면 -12.46
나는 가고 싶다 -13
.
해결책
int i = (int)floor(25.342);
다른 팁
int i = (int)floor(25.342);
이것은 12.99999로 변환됩니다.
ref :
여기서 X는 25.342입니다
int i = x> = 0? (int) (x+0.5) : (int) (x-0.5)
#include <math.h>
#include <stdio.h>
int main(){
double d = 25.342;
double e = -12.99;
printf("%d\n",(int)round(d)); // 25
printf("%d\n",(int)round(e)); // -13
return 0;
}
stdint.h를보고 싶을 수도 있습니다.
제휴하지 않습니다 StackOverflow