Home >Backend Development >C++ >How to prevent C `cout` from displaying numbers in scientific notation?

How to prevent C `cout` from displaying numbers in scientific notation?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-29 04:08:30547browse

How to prevent C   `cout` from displaying numbers in scientific notation?

How to Disable Scientific Notation in C cout

When handling large numbers in C , it can be desirable to display them in a readable format without the use of scientific notation. This can be achieved by modifying the behavior of the cout stream manipulator.

To display numbers with exact digits without scientific notation, use the std::fixed stream manipulator. This manipulator forces the output of floating-point values to be printed in fixed-point notation, preserving all digits.

Consider the following code snippet:

<br>double x = 1500;<br>for(int k = 0; k < 10; k  ){</p><pre class="brush:php;toolbar:false">double t = 0;
for(int i = 0; i < 12; i++){
    t += x * 0.0675;
    x += x * 0.0675;
}
cout << fixed << "Bas ana: " << x << "\tSon faiz: " << t << "\tSon ana: " << x + t << endl;

}

By using std::fixed, the output will appear as follows:

Bas ana: 3284.78        Son faiz: 1784.78        Son ana: 5069.55
Bas ana: 7193.17        Son faiz: 3908.4         Son ana: 11101.6
Bas ana: 15752         Son faiz: 8558.8         Son ana: 24310.8
Bas ana: 34494.5        Son faiz: 18742.5        Son ana: 53237
Bas ana: 75537.8        Son faiz: 41043.3        Son ana: 116581
Bas ana: 165417        Son faiz: 89878.7        Son ana: 255295
Bas ana: 362238        Son faiz: 196821         Son ana: 559059
Bas ana: 793246        Son faiz: 431009         Son ana: 1224255
Bas ana: 1737092       Son faiz: 943845         Son ana: 2680937
Bas ana: 3803972       Son faiz: 2066878        Son ana: 5870850

This output correctly displays the numbers with exact digits, preserving all significant figures.

The above is the detailed content of How to prevent C `cout` from displaying numbers in scientific notation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn