我有一双:

double d = 25.342;

如何将其转换为 25 值?

如果是 -12.46 我想要 -13

有帮助吗?

解决方案

int i = (int)floor(25.342);

其他提示

int i = (int)floor(25.342);

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

价:

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

其中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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top