1

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:

  • double -> long long
  • 234343.0 -> 234343
  • 3.41e10 -> 34100000000
  • 19.999999999 -> 19
  • -10.1 -> -10
  • -0.0000001 -> 0

Is there some beautiful way to complete my task?

Denis
  • 3,595
  • 12
  • 52
  • 86
  • 2
    Are you talking about converting strings containing floating point numbers to `long long` or actual `double` variables? – Columbo Nov 04 '14 at 22:04
  • @Columbo, Actually, I have double variable. But, if you need to cast double -> string -> long long, it is good too. – Denis Nov 04 '14 at 22:05
  • 2
    XY problem? Why should the e-notation matter? – cppguy Nov 04 '14 at 22:06
  • @cppguy, may be it is XY problem. My main task it transform double variable to integer array (this array is used for storing Big Numbers). – Denis Nov 04 '14 at 22:08

1 Answers1

3

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.

Columbo
  • 60,038
  • 8
  • 155
  • 203