题
我有一双:
double d = 25.342;
如何将其转换为 25
值?
如果是 -12.46
我想要 -13
。
解决方案
int i = (int)floor(25.342);
其他提示
int i = (int)floor(25.342);
请注意,这会将12.99999转换为12。
价:
其中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