C++ scientific Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

Definition

In C++, scientific makes a stream print floating-point numbers in scientific notation: one digit, a point, some decimals, and a power of ten. For example, 1500.0 prints as 1.500000e+03.

With scientific, setprecision counts the digits after the point. It is handy for very large and very small numbers. It comes from <ios>, and setprecision comes from <iomanip>.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::scientific into the stream before the numbers.

C++
std::cout << std::scientific << 12345.678;                    // 1.234568e+04
std::cout << std::scientific << std::setprecision(2) << 0.000123;   // 1.23e-04
CallWhat it does
std::cout << std::scientificScientific notation with 6 digits after the point.
std::cout << std::scientific << std::setprecision(2)Two digits after the point.
std::cout << std::defaultfloatGoes back to the default style.

Add std::uppercase to get E instead of e in the exponent.

Library declarations
C++
ios_base& scientific(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << scientific. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after scientific.

C++ Examples

Example 1 — Print a Number in Scientific Form

Compare the default with scientific.

C++
#include <iostream>

int main() {
    std::cout << 12345.678 << '\n';
    std::cout << std::scientific << 12345.678 << '\n';
    return 0;
}

How It Works

  1. By default six significant digits are printed, so 12345.678 becomes 12345.7.
  2. scientific writes it as a digit, a point, and a power of ten: 1.234568 times ten to the power 4.
  3. The e+04 means "times ten to the power of four".

Example 2 — Small Numbers with setprecision

Two digits after the point make a very small number short and readable.

C++
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::scientific << std::setprecision(2);

    std::cout << 0.000123 << '\n';
    std::cout << 6.02e23 << '\n';
    return 0;
}

How It Works

  1. setprecision(2) with scientific keeps two digits after the point.
  2. 0.000123 is 1.23 times ten to the power -4. The exponent shows -04.
  3. 6.02e23 is already in this form, so it prints as 6.02e+23.

Example 3 — Switch Between scientific and defaultfloat

std::defaultfloat restores the normal style.

C++
#include <iostream>

int main() {
    std::cout << std::scientific << 1000.0 << '\n';
    std::cout << std::defaultfloat << 1000.0 << '\n';
    return 0;
}

How It Works

  1. With scientific, 1000.0 prints as 1.000000e+03.
  2. std::defaultfloat turns the scientific setting off and goes back to the default style.
  3. The same value now prints as the short number 1000.

Frequently Asked Questions

It prints floating-point numbers in scientific notation, like 1.5e+03, which means 1.5 times 10 to the power 3.
Use setprecision(n). With scientific, n is the number of digits after the decimal point.
Add std::uppercase, so the exponent is written as E.
fixed always prints a plain decimal number. scientific always prints a digit, a point, decimals, and an exponent.
Use std::defaultfloat, or switch to std::fixed. cout.unsetf(std::ios::floatfield) works too.

Next: setbase()

Set the number base to 8, 10, or 16.

setbase() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful