如何将一个double舍入到C中最接近的较小的int?

时间:2009-10-27 15:20:22

标签: c double int

我有一双:

double d = 25.342;

如何将其转换为25值?

如果是-12.46,我想获得-13

4 个答案:

答案 0 :(得分:29)

int i = (int)floor(25.342);

答案 1 :(得分:15)

int i = (int)floor(25.342);

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

价:

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

答案 2 :(得分:1)

#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

答案 3 :(得分:0)

其中x是你的25.342

int i = x&gt; = 0? (int)(x + 0.5):( int)(x-0.5)