Advertisement

rounding vector

Started by April 09, 2021 05:17 PM
3 comments, last by pbivens67 3 years, 5 months ago

I am trying to round a double vector to 2 decimal places, but it prints out the number to 6 decimal places.

		for (int i = 0; i < count; i++)
		{
			cout << fixed << setw(4) << round(v_monthly_investment[i]) * 100 / 100 << " ";
			cout << fixed << setw(4) << round(v_yearly_rate[i]) * 100 / 100 << " ";
			cout << fixed << setw(4) << round(v_years[i]) * 100 / 100 << " ";
			cout << setw(2) << round(v_future_value[i]*100.0)/100.0 << endl;
		}

I used setprecision(2) and it worked thanks

Advertisement

Only the output is rounded, not the vector itself. A double has 15 significant digits always, you can only reduce the number of digits printed.

thanks I solved my problem

This topic is closed to new replies.

Advertisement