0

I have a simple function in C++.

float numret()
{
  return 10.00;
}

int main()
{

float a;
a = numret();
cout<<a;
}

This output value is 10 but it should be 10.00 as returned by the function. How can I make sure it is returning 10.00. I have also tried returning from a variable like this

float numret()
{
float maxval = 10.00;
return maxval;
}

but still I am getting 10 as output.

Ahmad Anis
  • 1,587
  • 2
  • 14
  • 40
  • You're confusing some concepts. First, it _is_ a `float`. `10` is a valid representation of a number stored by a `float`. Second, the number of decimal places in your source code has no relationship to the number of decimal places output by `cout`. It has no way of even knowing that count. You have to tell it what you want it to do. See the dupe for more on that. – Asteroids With Wings Apr 30 '20 at 18:33
  • 10 and 10.00 are actually the same number. You did in fact return 10 from the function. – eerorika Apr 30 '20 at 18:33
  • 2
    I think this will help u https://stackoverflow.com/questions/16280069/show-two-digits-after-decimal-point-in-c – Akash Bhogar Apr 30 '20 at 18:33
  • @AkashBhogar THANKS, it worked with setprecsion – Ahmad Anis Apr 30 '20 at 18:41

0 Answers0