Floating Point Default Print Format in C++

Last Updated : 29 Jan, 2026

Floating-point output formatting in C++ controls the representation of real numbers, including precision, notation (fixed/scientific), width, and alignment during stream insertion.

  • By default, C++ prints floating values with limited precision (6 significant digits), balancing readability and accuracy.
  • Manipulators like setprecision, fixed, and scientific allow you to change the display format.
C++
#include <iostream>
using namespace std;

int main() {
    double a = 1.23456789;
    double b = 123456789.0;
    cout << a << endl;
    cout << b << endl;
    return 0;
}

Output
1.23457
1.23457e+08

Floating-Point Output Behaviors:

1. No Trailing Zeros:

By default, floating-point numbers are printed without trailing zeros, providing a compact and readable output, though manual formatting may be needed for consistent width.

C++
#include <iostream>
using namespace std;

int main() {
    double a = 3.140000;
    double b = 5.000000;
    cout << a << endl;
    cout << b << endl;
    return 0;
}

Output
3.14
5

Explanation: The output is 3.14 and 5 because C++ omits unnecessary trailing zeros when printing floating-point numbers by default.

2. Precision Refers to Total Digits:

Precision in floating-point printing defines the total number of significant digits, not just the digits after the decimal point. This behavior emphasizes the value's overall accuracy rather than merely focusing on its decimal representation.

C++
#include <iostream>
using namespace std;

int main() {
    double a = 123.456789;
    double b = 0.000123456;
    cout << a << endl;
    cout << b << endl;
    return 0;
}

Output
123.457
0.000123456

Explanation: C++ uses a default precision of 6 significant digits, so 123.456789 is rounded to 123.457 while 0.000123456 is printed fully until six meaningful digits are reached.

3. Switching to Power Format:

If the integral part of a floating-point number exceeds the default precision (6 significant digits), C++ automatically switches to scientific notation to preserve significant digits. This behavior can be controlled using stream manipulators like fixed, setprecision, or scientific.

C++
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double a = 1234567.89;
    double b = 0.000123456;
    cout << a << endl;
    cout << b << endl;
    return 0;
}

Output
1.23457e+06
0.000123456

Explanation: When a number exceeds the default precision of 6 significant digits, it is automatically displayed in scientific (power) notation to keep the output compact and precise.

Comment