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.