blog
blog copied to clipboard
C++ Formatting Output: setprecision, fixed and showpoint Manipulator
Stream Manipulator | Description |
---|---|
setw(n) | Establishes a print field of n spaces. |
fixed | Displays floating-point numbers in fixed-point notation. |
showpoint | Causes a decimal point and trailing zeros to be displayed, even if there is no fractional part. |
setprecision(n) | Sets the precision of floating-point numbers. |
left | Causes subsequent output to be left-justified. |
right | Causes subsequent output to be right-justified. |
1. setprecision Manipulator
// This program demonstrates how setprecision rounds a floating-point value.
#include <iostream>
#include <iomanip> // Required for setprecision
using namespace std;
int main() {
double num = 1.23456789;
// Floating-point values may be rounded to a number of significant digits,
// or precision, which is the total number of digits that
// appear before and after the decimal point.
cout << num << endl; // 1.23457
// You can control the number of significant digits with which floating-point
// values are displayed by using the setprecision manipulator.
cout << setprecision(2) << num << endl; // 1.2
cout << setprecision(7) << num << endl; // 1.234568
cout << setprecision(10) << num << endl; // 1.23456789
cout << setprecision(15) << num << endl; // 1.23456789
cout << setprecision(17) << num << endl; // 1.2345678899999999
return 0;
}
2. fixed Manipulator
#include <iostream>
#include <iomanip> // Required for fixed
using namespace std;
int main() {
double gpa = 4.0;
cout << gpa << endl; // 4
// Stream manipulator, fixed, forces cout to print the digits
// in fixed-point notation, or decimal.
cout << fixed << gpa << endl; // 4.000000
return 0;
}
// combine setprecision and fixed
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x = 123.4567;
cout << x << endl; // 123.457
// The setprecision manipulator can sometimes surprise you in an undesirable way.
// When the precision of a number is ser to a lower value,
// numbers tend to be printed in scientific notation.
cout << setprecision(2) << x << endl; // 1.2e+02
cout << setprecision(2) << fixed << x << endl; // 123.46
return 0;
}
3. showpoint Manipulator
#include <iostream>
#include <iomanip> // Required for showpoint
using namespace std;
int main() {
double x = 123.4, y = 456.0;
// By default, floating-point numbers are not displayed with trailing zeros,
// and floating-point numbers that do not have a fractional part
// are not displayed with a decimal point.
cout << x << endl; // 123.4
cout << y << endl; // 456
cout << showpoint;
cout << x << endl; // 123.400
cout << y << endl; // 456.000
return 0;
}
// combine setprecision and showpoint
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x = 123.4, y = 456.0;
cout << x << endl; // 123.4
cout << y << endl; // 456
cout << setprecision(4) << showpoint << x << endl; // 123.4
cout << y << endl; // 456.0
return 0;
}
// combine setprecision, fixed and showpoint
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setprecision(2) << 25.00 << endl; // 25
cout << setprecision(2) << fixed << 25.00 << endl; // 25.00
cout << setprecision(0) << fixed << showpoint << 25.00; // 25.
return 0;
}
4. Reset Manipulator (Restore Defaults)
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const double BEEF_PRICE_PER_POUND = 3.14159265;
double pound;
double total_price;
cout << "How many pounds of beef did you buy? ";
cin >> pound;
cout << "Each pound of beef sells for $" << setprecision(9)
<< BEEF_PRICE_PER_POUND << endl;
total_price = BEEF_PRICE_PER_POUND * pound;
cout << "The total price is: $"
<< setprecision(2) << fixed << showpoint
<< total_price << endl;
// Reset iomanip:
cout << setprecision(6) << resetiosflags( ios::fixed | ios::showpoint);
cout << "\nYou have bought " << pound << " pound(s) of beef!" << endl;
return 0;
}
Output:
How many pounds of beef did you buy? 3
Each pound of beef sells for $3.14159265
The total price is: $9.42
You have bought 3 pound(s) of beef!