I want to get integer part
of double
. But this way does not work for me:
double param, fractpart, intpart;
param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%f = %f + %f \n", param, intpart, fractpart);
It is due to the fact, that double integer
may have e-notation
. For example, 3.41e10
is correct double number.
My test cases is:
Is there some beautiful way to complete my task?
Truncating your result by implicit conversion should do:
std::cout << (long long)234343.0 << '\n'
<< (long long)3.41e10 << '\n'
<< (long long)19.999999999 << '\n'
<< (long long)-10.1 << '\n'
<< (long long)-0.0000001 << '\n';
Should, on your machine, output
234343
34100000000
19
-10
0
Demo.