Jun 1, 2014 at 5:22am Jun 1, 2014 at 5:22am UTC
try just using printf or cout
but not both!
if you want to use cout
then look at this
http://www.cplusplus.com/reference/iomanip/setprecision/
1 2 3 4 5 6 7 8 9 10
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
float firstnumber = 256.78;
float totalnumber = 0;
totalnumber = firstnumber * firstnumber;
printf("total is %.2f" , totalnumber);
return 0;
}
output
total is 65935.97
Last edited on Jun 1, 2014 at 5:25am Jun 1, 2014 at 5:25am UTC
Jun 1, 2014 at 5:26am Jun 1, 2014 at 5:26am UTC
Ah so you have to tell printf how to format a variable by using the % symbol ?
S
Jun 1, 2014 at 3:29pm Jun 1, 2014 at 3:29pm UTC
256.78 * 256.78 gives exactly 65935.9684.
When you use cout <<
, the default precision is 6 significant digits which would be 65936
Thus, the displayed result has not omitted the fractional part. Rather it has correctly displayed the result to 6 significant digits.
Jul 30, 2014 at 5:42am Jul 30, 2014 at 5:42am UTC
Make sure your including the right things.
#include <iostream>
or really old version #include <iostream.h>
to use cout
, cin
and endl
.
#include <stdio.h>
to use printf
.
How to use printf:
printf("text %variable" ,number);
If you want to add more variables, keep adding at the back:
printf("text %variable1 %variable2" ,number1, number2);