You know when you format a string so that is displays a float, like so:
char buff[32];
float number = 1.23456789f;
sprintf_s(buff, 32, "My Number: %f", number);
You can specify the number of decimals to print after the point by using
%.2f
for two decimal places and
%.8f
for eight places and so on.
I'm not sure there's any built in formatting to limit the number of significant digits though. So 1234567 would become 123e4 and 0.00015678 would become 0.000157 if you wanted 3 significant digits. But you do have some easy control over the number of decimal places.
EDIT: If you're using cout then to limit the number of decimal places you'd need to do this:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float num = 1.23456f;
cout << "My Number: " << setprecision(1) << num;
return 0;
}
Where the output would then be "
My Number: 1.2
"
The number you pass to
setprecision
is the number of decimal places you want.