Home C++ Iostream Functions scientific C++ scientific Manipulator 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.
std::cout << std::scientific << 12345.678; // 1.234568e+04
std::cout << std::scientific << std::setprecision(2) << 0.000123; // 1.23e-04 Call What 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 ios_base& scientific(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << scientific. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after scientific.
C++ Examples 1. Basic 2. Small numbers 3. Go back Example 1 — Print a Number in Scientific Form Compare the default with scientific.
#include <iostream>
int main() {
std::cout << 12345.678 << '\n';
std::cout << std::scientific << 12345.678 << '\n';
return 0;
} How It Works By default six significant digits are printed, so 12345.678 becomes 12345.7. scientific writes it as a digit, a point, and a power of ten: 1.234568 times ten to the power 4.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.
#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 setprecision(2) with scientific keeps two digits after the point.0.000123 is 1.23 times ten to the power -4. The exponent shows -04.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.
#include <iostream>
int main() {
std::cout << std::scientific << 1000.0 << '\n';
std::cout << std::defaultfloat << 1000.0 << '\n';
return 0;
} How It Works With scientific, 1000.0 prints as 1.000000e+03. std::defaultfloat turns the scientific setting off and goes back to the default style.The same value now prints as the short number 1000. Frequently Asked Questions What does scientific do in C++? It prints floating-point numbers in scientific notation, like 1.5e+03, which means 1.5 times 10 to the power 3.
How do I control the digits in scientific notation? Use setprecision(n). With scientific, n is the number of digits after the decimal point.
How do I print a capital E? Add std::uppercase, so the exponent is written as E.
What is the difference between fixed and scientific? fixed always prints a plain decimal number. scientific always prints a digit, a point, decimals, and an exponent.
How do I turn scientific off? Use std::defaultfloat, or switch to std::fixed. cout.unsetf(std::ios::floatfield) works too.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion