# Thread: Controlling precision after E in sci notation

1. ## Controlling precision after E in sci notation

Hi,

I'm trying to print a number but it's printing like this:
1.0000e+000
but I want to only show two 0's after the e+, do you know of any manipulators or something I could use to restrict the numebr of digits?

I'm printing it right now using:
cout << setw(12) << right << scientific << setprecision(4) << F(i+1) << endl;

thanks,

Canadian

2. cplusplus.com states that three is the number of digits following the e, and the number of digits following the e shall be three. I can't find that language in the standard, but I also can't find anything that suggests it can be changed.

3. Okay, well if it's not possible, can anyone think of a way that I might be able to print a line e.g.
3.8045e+00 5.3859e+34 5.3853e+53

i.e. maybe is there a way where I can print columns of three values in this format by limiting the width given to each value so that the third digit after e+ gets cut off?

4. setw only sets minimum width, not maximum. Your best bet may be something like this (tested, and similar to code I've posted before):
Code:
```void print_with_two_exponents(std::ostream& os, float x, int precision) {
double lg = log10(fabs(x));
int exponent = static_cast<int>(floor(lg));
double mantissa = pow(10,lg-exponent);
if (x < 0) mantissa *= -1.0;
os << std::fixed << std::setprecision(precision);
os << mantissa << "e";
os << std::setw(3) << std::showpos;
os.setf(std::ios::internal, std::ios::adjustfield);
os << std::setfill('0') << exponent;
}```
Admittedly, this doesn't handle NaN's very well, but you can do what you want with those. You may also want to reset some of the flags before going back to where you were. If you only plan on using four digits after the decimal point, you can probably get away with using floats instead of doubles.

Popular pages Recent additions