17

我有一个双重:

double d = 25.342;

如何将其转换为25价值?

如果是-12.46我想得到-13

4

4 回答 4

33
int i = (int)floor(25.342);
于 2009-10-27T15:21:21.847 回答
16
int i = (int)floor(25.342);

请注意,这会将 12.99999 转换为 12。

参考:

http://www.codecogs.com/reference/c/math.h/floor.php

于 2009-10-27T15:25:38.950 回答
1

其中 x 是您的 25.342

整数 i = x >= 0 ?(int)(x+0.5) : (int)(x-0.5)

于 2009-10-27T15:26:50.933 回答
0
#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

于 2012-10-22T12:18:49.043 回答