Eliminating Scientific Notation in C cout
When dealing with large numerical values, C 's cout stream can output numbers using scientific notation (e.g., 1.23e 06). However, in certain situations, it is preferable to display numbers with precise decimal representation. Here's how to accomplish this:
Consider the following code that calculates compound interest:
<code class="cpp">double x = 1500; for (int k = 0; k<10 ; k++) { double t = 0; for (int i = 0; i<12; i++) { t += x * 0.0675; x += x * 0.0675; } cout << "Bas ana: " << x << "\tSon faiz: " << t << "\tSon ana: " << x + t << endl; }</code>
This code produces output in scientific notation, as seen in the following excerpt:
Bas ana: 1.73709e+006 Son faiz: 943845 Son ana: 2.68094e+006 Bas ana: 3.80397e+006 Son faiz: 2.06688e+006 Son ana: 5.87085e+006
To output numbers with exact decimal representation, utilize the std::fixed stream manipulator. This manipulator forces cout to use fixed-point notation for subsequent numeric output. Modify the code as follows:
<code class="cpp">cout << fixed << "Bas ana: " << x << "\tSon faiz: " << t << "\tSon ana: " << x + t << endl;</code>
This modification produces the desired output with precise decimal representation:
Bas ana: 1737090 Son faiz: 943845 Son ana: 2680935 Bas ana: 3803970 Son faiz: 2066880 Son ana: 5870850
The above is the detailed content of How to Eliminate Scientific Notation in C cout When Displaying Large Numbers?. For more information, please follow other related articles on the PHP Chinese website!