Home > Backend Development > C++ > body text

How Can I Prevent Scientific Notation When Displaying Large Floating-Point Numbers in C ?

DDD
Release: 2024-10-26 21:01:03
Original
993 people have browsed it

How Can I Prevent Scientific Notation When Displaying Large Floating-Point Numbers in C  ?

Resolving Scientific Notation in Cout Output

In C , the default precision and format specifiers for numeric output using cout often lead to scientific notation, especially for large floating-point numbers. To resolve this and display numbers with exact digits, use the std::fixed stream manipulator.

When using std::fixed, the decimal point is fixed to a default precision of 6, ensuring that numbers are displayed with decimals instead of scientific notation. To illustrate this in the provided code:

<code class="cpp">#include <iostream>
#include <iomanip>

int main() {
    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;
        }
        std::cout << std::fixed
                  << "Bas ana: " << x
                  << "\tSon faiz: " << t
                  << "\tSon ana: " << x + t
                  << std::endl;
    }
    return 0;
}</code>
Copy after login

By using std::fixed, the output now displays numbers with exact digits, eliminating scientific notation:

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.00     Son faiz: 8558.8       Son ana: 24310.8
Bas ana: 34494.50     Son faiz: 18742.5      Son ana: 53237.00
Bas ana: 75537.80     Son faiz: 41043.3      Son ana: 116581.00
Bas ana: 165417.00    Son faiz: 89878.7      Son ana: 255295.00
Bas ana: 362238.00    Son faiz: 196821.00    Son ana: 559059.00
Bas ana: 793246.00    Son faiz: 431009.00    Son ana: 1.22426e+006
Bas ana: 1.73709e+006 Son faiz: 943845.00    Son ana: 2.68094e+006
Bas ana: 3.80397e+006 Son faiz: 2.06688e+006 Son ana: 5.87085e+006
Copy after login

The above is the detailed content of How Can I Prevent Scientific Notation When Displaying Large Floating-Point Numbers in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!