Download presentation
Presentation is loading. Please wait.
Published byCameron Curtis Modified over 9 years ago
1
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output
2
CSE202: Lecture 8The Ohio State University2 Formatting Numbers for Output Number formatters are to be used in conjunction with cout For example, double x = 6; cout << x << endl; The above outputs 6, but x is declared a floating point number, so why isn’t 6.0000… displayed?
3
CSE202: Lecture 8The Ohio State University3 Formatting Numbers for Output (2) The decimal point is truncated for display, so we must force it to be shown. Observe the following code: double x = 6; cout.setf(ios::fixed); cout << x << endl; Notice the cout.setf(ios::fixed) command. It says “force cout to display the decimal point and 6 significant digits afterward.” Now 6.000000 will be displayed.
4
CSE202: Lecture 8The Ohio State University4 outputFormat1 // format output #include using namespace std; int main() { double x; cout << "Enter x: "; cin >> x; cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; return 0; }
5
CSE202: Lecture 8The Ohio State University5 > outputFormat1.exe Enter x: 123.45 x = 123.45 x = 123.450000 x = 1.234500e+02 > outputFormat1.exe Enter x: 1e15 x = 1e+15 x = 1000000000000000.000000 x = 1.000000e+15 … cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; …
6
CSE202: Lecture 8The Ohio State University6 > outputFormat1.exe Enter x: 1e-15 x = 1e-15 x = 0.000000 x = 1.000000e-15 > … cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; …
7
CSE202: Lecture 8The Ohio State University7 Setting Precision (1) Remember that fixed shows 6 decimal places by default. But… –if our program dealt with printing out dollar amounts, do we really need to show 6 decimal places? –Another example: if our program dealt with some rigorous scientific simulation, is 6 decimal places enough?
8
CSE202: Lecture 8The Ohio State University8 Setting Precision (2) To set the precision, we use cout.precision(n) formatter, where n is the number of decimal places to be displayed. double x = 6; cout.precision(3); cout.setf(ios::fixed); cout << x << endl; Now 6.000 is displayed.
9
CSE202: Lecture 8The Ohio State University9 examplePrecision // example of setting precision … int main() { cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl; return 0; }
10
CSE202: Lecture 8The Ohio State University10 > examplePrecision.exe pi/100 = 0.031416 pi/100 = 0.031 pi/100 = 0.031415926536 > … cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl; …
11
CSE202: Lecture 8The Ohio State University11 examplePrecision2 // example of setting precision … int main() { cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl; return 0; }
12
CSE202: Lecture 8The Ohio State University12 > examplePrecision2.exe pi/100 = 3.141593e-02 pi/100 = 3.142e-02 pi/100 = 3.141592653590e-02 > … cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl; cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl; …
13
CSE202: Lecture 8The Ohio State University13 examplePrecision3 // example of setting precision … int main() { cout.precision(30); cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl; return 0; } > examplePrecision3.exe pi/100 = 0.031415926535897934 >
14
CSE202: Lecture 8The Ohio State University14 examplePrecision4 // example of setting precision … int main() { cout.precision(30); cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl; return 0; } > examplePrecision4.exe pi/100 = 3.141592653589793394e-02 >
15
CSE202: Lecture 8The Ohio State University15 Double Precision IEEE Standard for Floating-Point Arithmetic: Double precision numbers have 16 significant digits.
16
CSE202: Lecture 8The Ohio State University16 outputFormat2 // format output #include using namespace std; int main() { double x; cout << "Enter x: "; cin >> x; cout.precision(12); cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; return 0; }
17
CSE202: Lecture 8The Ohio State University17 > outputFormat2.exe Enter x: 123.45 x = 123.45 x = 123.450000000000 x = 1.234500000000e+02 > outputFormat2.exe Enter x: 1e15 x = 1e+15 x = 1000000000000000.000000000000 x = 1.000000000000e+15 … cout.precision(12); cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; …
18
CSE202: Lecture 8The Ohio State University18 > outputFormat2.exe Enter x: 123456789e-17 x = 1.23456789e-09 x = 0.000000001235 x = 1.234567890000e-09 > … cout.precision(12); cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; …
19
CSE202: Lecture 8The Ohio State University19 Fixed vs. Scientific cout.precision(12); cout.setf(ios::fixed); cout << “x = “ << x << endl; Outputs 12 digits after the decimal place. cout.precision(12); cout.setf(ios::scientific); cout << “x = “ << x << endl; Outputs 13 significant digits.
20
CSE202: Lecture 8The Ohio State University20 Setting Output Width
21
tempTable CSE202: Lecture 8The Ohio State University21 > tempTable.exe fahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71 >
22
CSE202: Lecture 8The Ohio State University22 tempTableBad.cpp // Output with no formatting #include using namespace std; int main() { cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15; cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; } return 0; }
23
tempTableBad CSE202: Lecture 8The Ohio State University23 > tempTableBad.exe fahrenheit celsius kelvin -40 -40 233.15 -20 -28.8889 244.261 0 -17.7778 255.372 20 -6.66667 266.483 40 4.44444 277.594 60 15.5556 288.706 > cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15; cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }
24
CSE202: Lecture 8The Ohio State University24 tempTableBad2.cpp... int main() { cout.setf(ios::fixed); cout.precision(2); cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15; cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; } return 0; }
25
tempTableBad2 CSE202: Lecture 8The Ohio State University25 > tempTableBad2.exe fahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71 > cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15; cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }
26
Width Problem CSE202: Lecture 8The Ohio State University26 > tempTableBad2.exe fahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71 > cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; Problem: Need to fix the width of each output field.
27
CSE202: Lecture 8The Ohio State University27 #include Output can also be format using routines in iomanip. To use of these functions, include this file in your program header: #include
28
CSE202: Lecture 8The Ohio State University28 setw The formatter setw(n) sets the width of the output to at least n characters. String/value is right justified. Example: cout << setw(10)<< “Age” << setw(10) << “Weight” << endl; cout << setw(10) << 4 << setw(10) << 32 << endl; cout << setw(10) << 22 << setw(10) << 130.5 << endl; cout << setw(10) << 101 << setw(10) << 120 << endl; Output is: Age Weight 4 32 22 130.5 101 120
29
CSE202: Lecture 8The Ohio State University29 tempTable.cpp #include #include // Note: Must include iomanip... int main() { cout.setf(ios::fixed); cout.precision(2); cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15; cout << setw(6) << fahrenheit << setw(15) << celsius << setw(10) << kelvin << endl; }...
30
tempTable CSE202: Lecture 8The Ohio State University30 > tempTable.exe fahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71 > cout << setw(6) << fahrenheit << setw(15) << celsius << setw(10) << kelvin << endl;
31
trigTable CSE202: Lecture 8The Ohio State University31 > trigTable.exe degrees radians sin cos -180 -3.14 -0.00 -1.00 -150 -2.62 -0.50 -0.87 -120 -2.09 -0.87 -0.50 -90 -1.57 -1.00 0.00 -60 -1.05 -0.87 0.50 -30 -0.52 -0.50 0.87 0 0.00 0.00 1.00 30 0.52 0.50 0.87 60 1.05 0.87 0.50 90 1.57 1.00 0.00 120 2.09 0.87 -0.50 150 2.62 0.50 -0.87 180 3.14 0.00 -1.00 >
32
CSE202: Lecture 8The Ohio State University32 trigTable.cpp... #include // NOTE: Must include iomanip... cout.setf(ios::fixed); cout.precision(2); cout << setw(8) << "degrees" << setw(8) << "radians" << setw(8) << "sin" << setw(8) << "cos" << endl; for (int degrees = -180; degrees <= 180; degrees += 30) { double radians = degrees * M_PI/180.0; cout << setw(8) << degrees << setw(8) << radians << setw(8) << sin(radians) << setw(8) << cos(radians) << endl; }...
33
trigTable CSE202: Lecture 8The Ohio State University33 > trigTable.exe degrees radians sin cos -180 -3.14 -0.00 -1.00 -150 -2.62 -0.50 -0.87 -120 -2.09 -0.87 -0.50 -90 -1.57 -1.00 0.00 -60 -1.05 -0.87 0.50 -30 -0.52 -0.50 0.87 0 0.00 0.00 1.00 30 0.52 0.50 0.87 60 1.05 0.87 0.50 90 1.57 1.00 0.00 120 2.09 0.87 -0.50 150 2.62 0.50 -0.87 180 3.14 0.00 -1.00 > cout << setw(8) << "degrees" << setw(8) << "radians" << setw(8) << "sin" << setw(8) << "cos" << endl;
34
expTable CSE202: Lecture 8The Ohio State University34 > expTable.exe Enter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00 >
35
CSE202: Lecture 8The Ohio State University35 expTable.cpp... #include // NOTE: Must include iomanip... cout << "Enter number of values: "; cin >> n; cout.setf(ios::fixed); cout.precision(2); cout << setw(5) << "x" << setw(10) << "e^x" << setw(10) << "2^x" << endl; for (int i = 1; i <= n; i++) { double x = i; cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl; }...
36
expTable CSE202: Lecture 8The Ohio State University36 > expTable.exe Enter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00 > cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;
37
expTable CSE202: Lecture 8The Ohio State University37 > expTable.exe Enter number of values: 15 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 11 59874.14 2048.00 12 162754.79 4096.00 13 442413.39 8192.00 141202604.28 16384.00 153269017.37 32768.00 > cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;
38
expTable CSE202: Lecture 8The Ohio State University38 > expTable.exe Enter number of values: 25 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 13 442413.39 8192.00 141202604.28 16384.00 153269017.37 32768.00... 239744803446.258388608.00 2426489122129.8416777216.00 2572004899337.3933554432.00 > cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;
39
CSE202: Lecture 8The Ohio State University39 expTable2.cpp... cout << "Enter number of values: "; cin >> n; cout.setf(ios::fixed); cout.precision(2); cout << setw(5) << "x" << " " << setw(10) << "e^x" << " " << setw(10) << "2^x" << endl; for (int i = 1; i <= n; i++) { double x = i; // Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl; }...
40
expTable2 CSE202: Lecture 8The Ohio State University40 > expTable2.exe Enter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00 > // Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;
41
expTable2 CSE202: Lecture 8The Ohio State University41 > expTable2.exe Enter number of values: 15 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 10 22026.47 1024.00 11 59874.14 2048.00 12 162754.79 4096.00 13 442413.39 8192.00 14 1202604.28 16384.00 15 3269017.37 32768.00 > // Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;
42
expTable2 CSE202: Lecture 8The Ohio State University42 > expTable2.exe Enter number of values: 25 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 20 485165195.41 1048576.00 21 1318815734.48 2097152.00 22 3584912846.13 4194304.00 23 9744803446.25 8388608.00 24 26489122129.84 16777216.00 25 72004899337.39 33554432.00 > // Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;
43
CSE202: Lecture 8The Ohio State University43 expTable3.cpp... cout << "Enter number of values: "; cin >> n; // DO NOT SET FIXED OUTPUT FORMAT // cout.setf(ios::fixed); cout.precision(2); cout << setw(5) << "x" << setw(10) << "e^x" << setw(10) << "2^x" << endl; for (int i = 1; i <= n; i++) { double x = i; cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl; }...
44
expTable3 CSE202: Lecture 8The Ohio State University44 > expTable3.exe Enter number of values: 25 1 2.7 2 2 7.4 4... 20 4.9e+08 1e+06 21 1.3e+09 2.1e+06 22 3.6e+09 4.2e+06 23 9.7e+09 8.4e+06 24 2.6e+10 1.7e+07 25 7.2e+10 3.4e+07 > cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;
45
expTable3 CSE202: Lecture 8The Ohio State University45 > expTable3.exe Enter number of values: 10 x e^x 2^x 1 2.7 2 2 7.4 4 3 20 8 4 55 16 5 1.5e+02 32 6 4e+02 64 7 1.1e+03 1.3e+02 8 3e+03 2.6e+02 9 8.1e+03 5.1e+02 10 2.2e+04 1e+03 > cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;
46
CSE202: Lecture 8The Ohio State University46 Other Formatters Iomanip contains many other formatters, including: fixed Force show a decimal point with 6 trailing decimal places scientific Outputs floating point number in scientific notation hex Outputs integer in hexadecimal (base16) oct Outputs integer in octal (base 8) showpos Force positive numbers to be shown with a leading ‘+’ symbol setprecision(n) Set floating point precision to n places setw(n) Set the field width to n characters
47
CSE202: Lecture 8The Ohio State University47 outputFormat3 // format output #include using namespace std; int main() { double x; cout << "Enter x: "; cin >> x; // general format, default, 12 significant digits cout << "x = " << setprecision(12) << x << endl; // fixed decimal notation, 12 digits after decimal point cout << "x = " << fixed << setprecision(12) << x << endl; // scientific notation, 12 digits after decimal point cout << "x = " << scientific << setprecision(12) << x << endl; return 0; }
48
CSE202: Lecture 8The Ohio State University48 … // general format, default, 12 significant digits cout << "x = " << setprecision(12) << x << endl; // fixed decimal notation, 12 digits after decimal point cout << "x = " << fixed << setprecision(12) << x << endl; // scientific notation, 12 digits after decimal point cout << "x = " << scientific << setprecision(12) << x << endl; … > outputFormat3.exe Enter x: 123.45 x = 123.45 x = 123.450000000000 x = 1.234500000000e+02 > outputFormat3.exe Enter x: 123456789e-17 x = 1.23456789e-09 x = 0.000000001235 x = 1.234567890000e-09
49
CSE202: Lecture 8The Ohio State University49 Rounding Output // example of output rounding #include using namespace std; int main() { double x = 1.0/7.0; cout << "x = " << setprecision(12) << x << endl; cout << "x = " << setprecision(7) << x << endl; cout << "x = " << setprecision(5) << x << endl; return 0; } C++ rounds output.
50
CSE202: Lecture 8The Ohio State University50 … double x = 1.0/7.0; cout << "x = " << setprecision(12) << x << endl; cout << "x = " << setprecision(7) << x << endl; cout << "x = " << setprecision(5) << x << endl; … > roundOutput.exe x = 0.142857142857 x = 0.1428571 x = 0.14286 >
51
expTable CSE202: Lecture 8The Ohio State University51 > expTable.exe Enter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00 >
52
expTable4 CSE202: Lecture 8The Ohio State University52 > expTable4.exe Enter number of values: 10 x e^x 2^x 1 2.72 2 2 7.39 4 3 20.09 8 4 54.60 16 5 148.41 32 6 403.43 64 7 1096.63 128 8 2980.96 256 9 8103.08 512 10 22026.47 1024 >
53
CSE202: Lecture 8The Ohio State University53 expTable4.cpp... cout << "Enter number of values: "; cin >> n; cout.setf(ios::fixed); cout << setw(5) << "x" << " " << setw(10) << "e^x" << " " << setw(8) << "2^x" << endl; for (int i = 1; i <= n; i++) { double x = i; // Use precision 2 for e^x and precision 0 (integer) for 2^x. cout << setw(5) << i << " " << setw(10) << setprecision(2) << exp(x) << " " << setw(8) << setprecision(0) << pow(2.0,x) << endl; }...
54
expTable CSE202: Lecture 8The Ohio State University54 > expTable4.exe Enter number of values: 10 x e^x 2^x 1 2.72 2 2 7.39 4 3 20.09 8 4 54.60 16 5 148.41 32 6 403.43 64 7 1096.63 128 8 2980.96 256 9 8103.08 512 10 22026.47 1024 > // Use precision 2 for e^x and precision 0 (integer) for 2^x. cout << setw(5) << i << " " << setw(10) << setprecision(2) << exp(x) << " " << setw(8) << setprecision(0) << pow(2.0,x) << endl;
55
Summary cout.setf(ios::fixed); // use fixed decimal notation cout.setf(ios::scientific); // use scientific notation cout.precision(k); // print k digits after decimal iomanip: #include // include IO manipulators (formatters) cout << setw(k) <<...; // set next field width to k cout << setprecision(k) <<...; // print k digits after decimal cout << fixed <<...; // switch to fixed decimal notation cout << scientific <<...; // switch to scientific notation CSE202: Lecture 8The Ohio State University55
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.