Exponential

D

diegoandrade

In this code the exponential part using scientific notation becomes
e+002 is ther a way of changging the number of values after e so they
can appear like e+02, only 2 values instead 3??

I have been thinking a lot about this!!!
any comments


// modify floatfield using manipulators
#include <iostream>
using namespace std;

int main () {
float f;
f=365.25;
cout << fixed << f << endl;
cout << scientific << f << endl;
return 0;
}

The execution of this example shall display:
365.250000
3.652500e+002
 
V

Victor Bazarov

In this code the exponential part using scientific notation becomes
e+002 is ther a way of changging the number of values after e so they
can appear like e+02, only 2 values instead 3??

There is no way using C++ manipulators only, if that's what you're
asking.

The reason is in the C library. C++ specifies that 'scientific' shall
cause the output to be the same as %e (or %E if 'uppercase' is set) in
fprintf. In C++ %e causes output of as many digits in the exponent as
necessary to represent the exponent. On your system 'double' (and that
is what your 'float' is converted to) has probably three digits of the
exponent, and that's why it prints e+002.

The only way to achieve 2 digits in your output is to perform your own
conversion to, say, a string, and then output that string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top