2

im trying to use the % operator on a double in c++, i have done the same in java and it works fine.

is there something im missing here or is this not allowed, sorry im new to c++ so might be making a really stupid error here

    double i =  full_price_in_pence / 100.0;
    double j = full_price_in_pence % 100;
    int final_pounds = (int) i;
    int final_pence = (int) j;

and these are both double values

full_price_in_pence
full_price_in_pounds
AngryDuck
  • 4,358
  • 13
  • 57
  • 91

4 Answers4

9

You should use the std::fmod() function from the <cmath> Standard header:

#include <cmath>

// ...

double j = fmod(full_price_in_pence, 100);
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
3

% is for integers only, you're looking for fmod.

Femaref
  • 60,705
  • 7
  • 138
  • 176
2

You cannot use % operator for a double variable. Only int variables are allowed to do that.

You can check some good answers from another question like this; you can find them here.

Community
  • 1
  • 1
George Netu
  • 2,758
  • 4
  • 28
  • 49
1

No, it's not allowed. Operands of the % operator must be of integral types. Use std::fmod() instead.