C++/Console/cout setf

Материал из C\C++ эксперт
Перейти к: навигация, поиск

cout: how to display float number, ios::showpoint, ios::showpos

 
#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::showpoint);
  cout.setf(ios::showpos);
  cout << 100.0; 
  return 0;
}


cout.setf(ios::hex)

  
#include <iostream>
using namespace std; 
int main(void) {
   cout.setf(ios::hex);
   cout << 100;
   cout.setf(ios::showbase);
   cout << 100;
}


cout.setf(ios::showbase)

  
#include <iostream>
using namespace std; 
int main(void) {
   cout.setf(ios::hex);
   cout << 100;
   cout.setf(ios::showbase);
   cout << 100;
}


cout.setf(ios::showpos | ios::showpoint) and cout.setf(ios::showpoint, ios::showpos | ios::showpoint)

  
#include <iostream>
using namespace std; 
int main(void){
   cout.setf(ios::showpos | ios::showpoint);
   cout << 100 << endl;
   cout.setf(ios::showpoint, ios::showpos | ios::showpoint);
   cout << 100;
}


cout.setf(ios::uppercase | ios::scientific)

  
#include <iostream>
using namespace std; 
int main(void) {
   cout.setf(ios::uppercase | ios::scientific);
   cout << 100.12;
   cout.unsetf(ios::uppercase);
   cout << endl << 100.12;
}


Set cout: hex, basefield, setf(ios::hex, ios::basefield)

 
#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::hex, ios::basefield);
  cout << 100;
  return 0;
}


Set cout: setf(ios::showpoint | ios::showpos, ios::showpoint)

 
#include <iostream>
using namespace std;
int main( )
{
  cout.setf(ios::showpoint | ios::showpos, ios::showpoint);
  cout << 100.0;
  return 0;
}


You can OR together two or more flags: ios::uppercase | ios::scientific

 

#include <iostream>
using namespace std;
int main()
{
  cout.setf(ios::uppercase | ios::scientific);
  cout << 1001.12;                    
  cout.unsetf(ios::uppercase);       // clear uppercase
  cout << " \n" << 1100.12;           
  return 0;
}