hello, i just had a very simple question about float, so i am not going to post the full coding, only what matters in this instance.

my output of program shows the computed number as 4, instead of 4.00, even though it is declared as float

Here is the example:

int sum, num1, num2, num3, num4;
float mean;

sum = num1 + num2 + num3 + num4;
mean = sum / 4.0;


The 4 nums are integers entered by the user in my code and the output shows:

"The mean is 4" when i want it to be the mean is 4.00, or rounded to the nearest 2 decimals.

any ideas what the problem is?

Recommended Answers

All 11 Replies

hello, i just had a very simple question about float, so i am not going to post the full coding, only what matters in this instance.

my output of program shows the computed number as 4, instead of 4.00, even though it is declared as float

Here is the example:

int sum, num1, num2, num3, num4;
float mean;

sum = num1 + num2 + num3 + num4;
mean = sum / 4.0;


The 4 nums are integers entered by the user in my code and the output shows:

"The mean is 4" when i want it to be the mean is 4.00, or rounded to the nearest 2 decimals.

any ideas what the problem is?

lookup the <iomanip> library. In particular, you will be interested in the setprecision() and setw() functions.

i have tried to enter the setprecision in already like this:

cout << "The mean is: " << setprecision(3) << mean << endl;

the mean output still displays 4

i have tried to enter the setprecision in already like this:

cout << "The mean is: " << setprecision(3) << mean << endl;

the mean output still displays 4

i do my set precision like...

cout.setf(fixed::ios);
cout.setprecision(3);
cout<<"the mean is: "<<mean<<endl;

this should make it only 3, to change it to 2, change the number 3 to 2.

is there any other way then setprecision to just show the decimals?..all i want is it to show 4.003434 whatever instead of just 4

is there any other way then setprecision to just show the decimals?..all i want is it to show 4.003434 whatever instead of just 4

you could change all your int's into double's. though i am sure you knew that.

also, you do not have to use the cout.setf(fixed::ios) you can just use the second line down in my code but it will count the decimal as a number also.

i set all of my nums to double like this and 4 still shows in the output.

double num1, num2, num3, num4
float sum, mean;

cin >> num1 >> num2 >> num3 >> num4

sum = num1 + num2 + num3 + num4;
mean = sum / 4.0


cout << " The mean is: " << mean << endl;

setprecision does not work.

i set all of my nums to double like this and 4 still shows in the output.

double num1, num2, num3, num4
float sum, mean;

cin >> num1 >> num2 >> num3 >> num4

sum = num1 + num2 + num3 + num4;
mean = sum / 4.0


cout << " The mean is: " << mean << endl;

setprecision does not work.

you want to place the setprecision right above the cout you place the mean. this is because it only works for one cout statement

This works too #include <iomanip> cout << " The mean is: " <<fixed<<setprecision(2)<< mean << endl; . It doesn't have to be in the statement itself, it can be beforehand.

i do my set precision like...

cout.setf(fixed::ios);
cout.setprecision(3);
cout<<"the mean is: "<<mean<<endl;

this should make it only 3, to change it to 2, change the number 3 to 2.

I think you meant ios::fixed . This is also how I handle number formatting.

I think you meant ios::fixed . This is also how I handle number formatting.

ya, my bad

alright it works now with the << fixed <<

thanks a lot guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.