Archived

This topic is now archived and is closed to further replies.

Jx

std::cout and floats......

Recommended Posts

use "precision" or "setprecision":
  
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
float f = 3.14159f;

cout << f << endl; // default precision

cout.precision (4); // change default

cout << f << endl; // print with new default

cout << setprecision (3) << f << endl; // also changes default

cout << f << endl;
return 0;
}


3.14159
3.142
3.14
3.14

Share this post


Link to post
Share on other sites