C convert floating point to int
199,464
Solution 1
my_var = (int)my_var;
As simple as that. Basically you don't need it if the variable is int.
Solution 2
Use in C
int C = var_in_float;
They will convert implicit
Solution 3
If you want to round it to lower, just cast it.
float my_float = 42.8f;
int my_int;
my_int = (int)my_float; // => my_int=42
For other purpose, if you want to round it to nearest, you can make a little function or a define like this:
#define FLOAT_TO_INT(x) ((x)>=0?(int)((x)+0.5):(int)((x)-0.5))
float my_float = 42.8f;
int my_int;
my_int = FLOAT_TO_INT(my_float); // => my_int=43
Be careful, ideally you should verify float is between INT_MIN and INT_MAX before casting it.
Solution 4
double a = 100.3;
printf("%f %d\n", a, (int)(a* 10.0));
Output Cygwin 100.3 1003
Output MinGW: 100.3 1002
Using (int) to convert double to int seems not to be fail-safe
You can find more about that here: Convert double to int?
Comments
-
A. O. almost 2 years
I'm using C (not C++).
I need to convert a float number into an
int
. I do not want to round to the the nearest number, I simply want to eliminate what is after the integer part. Something like4.9 -> 4
.9-> 4 -
A. O. almost 10 yearsReally thanks! It worked! Sorry if the question was too simple, i am a newbie in c...
-
Jakub almost 10 yearsMake sure you do what you want/should for negative and large numbers. Things may be more complicated. If the floats are positive and smaller than the maximum integer on your platform, you ara safe.
-
saolof over 4 yearsThis is nice, but it is also undefined behaviour.
-
DownloadPizza almost 4 yearsUsing Macros with logic is not incredibly smart, as people tend to view them as functions. For example if you were to
FLOAT_TO_INT(rand())
it would behave weird, better to make it a function -
phuclv over 3 yearspow returns a double, that's why you can't print it using
%d
. And using pow for that purpose is wrong because it may return incorrect results for integers. Use 1e4 instead to get 10^4. See Why does gcc compiler output pow(10,2) as 99 not 100?, Why pow(10,5) = 9,999 in C++, Why does pow(5,2) become 24? -
user3629249 over 3 yearsregarding:
#define FLOAT_TO_INT(x) ((x)>=0?(int)((x)+0.5):(int)((x)-0.5))
Those literals0.5
is adouble
You really wantfloat
. Suggest: `#define FLOAT_TO_INT(x) ((x)>=0.0f?(int)((x)+0.5f):(int)((x)-0.5f)) -
user3629249 over 3 yearsunfortunately, the question is about converting
float
toint
, notdouble
toint
-
chux - Reinstate Monica over 3 years" round it to lower" is incorrect/unclear for negative values.
(int)
truncates the fraction. -
chux - Reinstate Monica over 3 years@user3629249
(x)+0.5f
can give incorrect rounded results when the sum is inexact.(x)+0.5
avoids this by using widerdouble
math. IAClroundf()
,roundf()
is better. -
chux - Reinstate Monica over 3 years@user3629249 Same issue applies with
float
, yet afloat
only example would be better here. -
Tomi Ollila over 3 yearsOOh, this (int)14e was great. Is it safe? 1e4 w/o casting has type 'double' (as the pow() case)
-
Tomi Ollila over 3 yearsLost change to edit above comment more -- just recently I had use case for
1e6
, but (as usual) I usedprintf("%d\n", 1e6);
to test it out and got weird results. ifconst int million = 1e6
is found safe I start using such a format. -
chux - Reinstate Monica over 3 years
1e6
relies on the compiler to make a qualitydouble
toint
conversion. Less likely a problem thanpow()
, yet I see no C spec guaranteeing the best answer. Alternatives to(int)1e6
: Why write 1,000,000,000 as 1000*1000*1000 in C? and why not safe -
Tomi Ollila over 3 yearsyes
pow()
is bad, compared to10e4
in this example (have to fix, with edit commit). I wondered why that10e4
isdouble
notint
, then tried1.12e1
and12e-3
to understand. -
Viraj Mohite about 2 yearsSimple and short