Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,

Similar presentations


Presentation on theme: "CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,"— Presentation transcript:

1 CSE1222: Lecture 9The Ohio State University1

2 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? CSE1222: Lecture 9The Ohio State University2

3 Formatting Numbers for Output (2)  The decimal point is truncated for display, so we must force it to be shown: 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 CSE1222: Lecture 9The Ohio State University3

4 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; } CSE1222: Lecture 9The Ohio State University4

5 > 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 CSE1222: Lecture 9The Ohio State University5 … 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 > outputFormat1.exe Enter x: 1e-15 x = 1e-15 x = 0.000000 x = 1.000000e-15 > CSE1222: Lecture 9The Ohio State University6 … 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 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? CSE1222: Lecture 9The Ohio State University7

8 Setting Precision (2)  The cout.precision(n) formatter Set the precision 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 CSE1222: Lecture 9The Ohio State University8

9 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; } CSE1222: Lecture 9The Ohio State University9

10 > examplePrecision.exe pi/100 = 0.031416 pi/100 = 0.031 pi/100 = 0.031415926536 > CSE1222: Lecture 9The Ohio State University10 … 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 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; } CSE1222: Lecture 9The Ohio State University11

12 > examplePrecision2.exe pi/100 = 3.141593e-02 pi/100 = 3.142e-02 pi/100 = 3.141592653590e-02 > CSE1222: Lecture 9The Ohio State University12 … 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 examplePrecision3 // example of setting precision … int main() { cout.precision(30); cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl; return 0; } CSE1222: Lecture 9The Ohio State University13 > examplePrecision3.exe pi/100 = 0.031415926535897934 >

14 examplePrecision4 // example of setting precision … int main() { cout.precision(30); cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl; return 0; } CSE1222: Lecture 9The Ohio State University14 > examplePrecision4.exe pi/100 = 3.141592653589793394e-02 >

15 Double Precision  IEEE Standard for Floating-Point Arithmetic: Double precision numbers have 16 significant digits CSE1222: Lecture 9The Ohio State University15

16 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; } CSE1222: Lecture 9The Ohio State University16

17 > 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 CSE1222: Lecture 9The Ohio State University17 … 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 > outputFormat2.exe Enter x: 123456789e-17 x = 1.23456789e-09 x = 0.000000001235 x = 1.234567890000e-09 > CSE1222: Lecture 9The Ohio State University18 … 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 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. CSE1222: Lecture 9The Ohio State University19

20 CSE1222: Lecture 9The Ohio State University20

21 Formatted Writing to Files Everything we can do with cout also applies to fout Including I/O manipulation: setprecision(), setw(), etc. CSE1222: Lecture 9The Ohio State University21

22 writeFile2.cpp #include // function exit() is in cstdlib #include // class ofstream() is in fstream #include using namespace std; int main() { ofstream fout; // declare an output file stream fout.open("sample.txt", ios::out); // open file file_name for output if (!fout.is_open()) // check if file is opened for output { cerr << "Unable to open file sample.txt." << endl; exit(10); } CSE1222: Lecture 9The Ohio State University22

23 writeFile2.cpp (cont) cout << "Writing to file sample.txt." << endl; // write to the file fout << "Test file output." << endl; fout << "100.0/3.0 = " << 100.0/3.0 << endl; fout.precision(12); fout << "100.0/3.0 = " << 100.0/3.0 << endl; fout << "100.0/3.0 = " << fixed << 100.0/3.0 << endl; // close file stream fout fout.close(); return 0; } CSE1222: Lecture 9The Ohio State University23

24 CSE1222: Lecture 9The Ohio State University24

25 tempTable CSE1222: Lecture 9The Ohio State University25 > 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 >

26 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; } CSE1222: Lecture 9The Ohio State University26

27 tempTableBad CSE1222: Lecture 9The Ohio State University27 > 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; }

28 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; } CSE1222: Lecture 9The Ohio State University28

29 tempTableBad2 CSE1222: Lecture 9The Ohio State University29 > 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; }

30 Width Problem CSE1222: Lecture 9The Ohio State University30 > 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.

31 #include  Output can also be formatted using routines in iomanip  To use of these functions, include this file in your program header: #include CSE1222: Lecture 9The Ohio State University31

32 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 CSE1222: Lecture 9The Ohio State University32

33 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; }... CSE1222: Lecture 9The Ohio State University33

34 tempTable CSE1222: Lecture 9The Ohio State University34 > 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;

35 trigTable CSE1222: Lecture 9The Ohio State University35 > 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 >

36 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; }... CSE1222: Lecture 9The Ohio State University36

37 trigTable CSE1222: Lecture 9The Ohio State University37 > 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 ;

38 expTable CSE1222: Lecture 9The Ohio State University38 > 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 >

39 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; }... CSE1222: Lecture 9The Ohio State University39

40 expTable CSE1222: Lecture 9The Ohio State University40 > 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;

41 expTable CSE1222: Lecture 9The Ohio State University41 > 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;

42 expTable CSE1222: Lecture 9The Ohio State University42 > 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;

43 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; }... CSE1222: Lecture 9The Ohio State University43

44 expTable2 CSE1222: Lecture 9The Ohio State University44 > 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;

45 expTable2 CSE1222: Lecture 9The Ohio State University45 > 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;

46 expTable2 CSE1222: Lecture 9The Ohio State University46 > 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;

47 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; }... CSE1222: Lecture 9The Ohio State University47

48 expTable3 CSE1222: Lecture 9The Ohio State University48 > 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;

49 expTable3 CSE1222: Lecture 9The Ohio State University49 > 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;

50 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 CSE1222: Lecture 9The Ohio State University50

51 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; } CSE1222: Lecture 9The Ohio State University51

52 > 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 CSE1222: Lecture 9The Ohio State University52 … // 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; …

53 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; } CSE1222: Lecture 9The Ohio State University53 C++ rounds output

54 > roundOutput.exe x = 0.142857142857 x = 0.1428571 x = 0.14286 > CSE1222: Lecture 9The Ohio State University54 … double x = 1.0/7.0; cout << "x = " << setprecision(12) << x << endl; cout << "x = " << setprecision(7) << x << endl; cout << "x = " << setprecision(5) << x << endl; …

55 expTable CSE1222: Lecture 9The Ohio State University55 > 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 >

56 expTable4 CSE1222: Lecture 9The Ohio State University56 > 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 >

57 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; }... CSE1222: Lecture 9The Ohio State University57

58 expTable CSE1222: Lecture 9The Ohio State University58 > 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;

59 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 CSE1222: Lecture 9The Ohio State University59


Download ppt "CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,"

Similar presentations


Ads by Google