Formatting Floating-Point Numbers

Example 1:

#include "iostream.h"
#include "iomanip.h" 
  
int main() {  
  float x = 23.756;  
  float y = 123.54321;  
  
  cout << setiosflags (ios::showpoint | ios::fixed) << setprecision(2);  
  cout << setw(8) << x << endl << setw(8) << y << endl;  
     
  return 0;  
} 
 
Output: 
 
   23.76 
  123.54 
 
 

Example 2:
 
#include "iostream.h"  
#include "iomanip.h"
  
int main() {  
  float x = 23.756;  
  float y = 123.54321;  
  
  cout << setiosflags (ios::showpoint | ios::fixed) << setprecision(2);  
  cout << "$"<< x << endl << "$" << y << endl;  
     
  return 0;  
} 
 
Output: 
 
$23.76 
$123.54 

Chris Alvin
Last modified: Mon Nov 1 12:57:02 CST 1999