Pergunta

Eu tenho um double:

double d = 25.342;

Como posso convertê-lo em valor 25?

Se fosse -12.46 eu gostaria de obter -13.

Foi útil?

Solução

int i = (int)floor(25.342);

Outras dicas

int i = (int)floor(25.342);

Note que este irá converter 12,99999-12.

Ref:

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

Onde x é o seu 25,342

i int = 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;
}

Você também pode querer dar uma olhada em stdint.h

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top