How to get non scientific output while still not outputing zeros in std::cout?

Viewed 44

Here's is an example:

std::cout << 1000000.0 << "\n";
std::cout << std::fixed << 1000000.0 << "\n";
std::cout << std::fixed << std::noshowpoint << 1000000.0 << "\n";

It will print me this:

1e+06
1000000.000000
1000000.000000

But I want this:

1000000

How I can do it with IO manipulators without setprecision()? Why without? Because I want to print:

std::cout << 1000000.0 << " " << 1111111.1 << " " << 1234567.89;

and get:

1000000 1111111.1 1234567.89

without calculating precision for every number

1 Answers

The scientific output is used when the digits count of the number is higher than precision. For example, the default precision is equal to 6, but the number 1234567 has 7 digits, so it will be printed as 1.23457+e06, cutting off the last digit. So to get 1234567 printed normally we need at least precision = 7. The highest precision that is not adding the "wrong" numbers is 15, so you need to use std::setprecision(15).

code: std::cout << std::setprecision(15) << 1000000.0 << " " << 1111111.1 << " " << 1234567.89;

out: 1000000 1111111.1 1234567.89

Related