tags:

views:

467

answers:

3

I've got a double... say

double d = 25.342;

How can I get the 25 value? ( If it were -12.46 I'd like to get -13)

Thanks! Manuel

+12  A: 
int i = (int)floor(25.342);
AraK
I'd like the result to be of type int
Manuel
The problems start when the argument to floor is > INT_MAX or < INT_MIN.
dirkgently
In that case, you can't actually get an integer version of the floor of the value, so it doesn't really matter.
jprete
+5  A: 
int i = (int)floor(25.342);

Note this will convert 12.99999 to 12.

Ref:

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

soru
Upvoted for reference link, but you don't need the cast to the int there, floor already returns as an int?
Lee
`floor( )` returns a double (§7.12.9.2 in the C standard).
Stephen Canon
I sit corrected! Thanks :]
Lee
+1  A: 

Where x is your 25.342

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

Mike
@Mike I don't think he wants to round numbers. Look at the negative number example :)
AraK
I did, his post said "( If it were -12.46 I'd like to get -13)" So I presumed a little rounding.
Mike
That's rounding to lower, or, essentially, floor().
David Thornley
Wouldn't rounding -12.46 give you and integer value of -12 and not -13?
semaj
Not with the check for negativity.
Mike
Rounding -12.46 to closest integer will produce -12, as will rounding closer to zero. Rounding to lower will give you -13, and this is what the questioner wants.
David Thornley