-1

So I'm trying my hand at C++ again and I'm writing a function that keeps giving the wrong value.

I've boiled it down to a line basically this: double testD = (10/100); that gives me 0 instead of 0.1.

I might have missed something major but I thought double held decimal values? For the life of me I can't get this to be correct.

Please help me (even if I'm terribly stupid).

Mowen
  • 11
  • 2

1 Answers1

0

10 and 100 are integers. This will result in integer division, which will evaluate to 0. That 0 will then be stored in testD.

To force floating-point division cast one of the numbers to float or double.

((double)whatever)/something
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40