Pregunta

Tengo un doble:

double d = 25.342;

¿Cómo puedo convertirlo al valor 25 ?

Si fuera -12.46 me gustaría obtener -13 .

¿Fue útil?

Solución

int i = (int)floor(25.342);

Otros consejos

int i = (int)floor(25.342);

Tenga en cuenta que esto convertirá 12.99999 a 12.

Ref:

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

Donde x es tu 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;
}

También es posible que desee echar un vistazo a stdint.h

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top