exponent format.

A

adahili

Hi,

I use cout to print numbers in scientific notation. I noticed that a
number like 12.34 is sometimes (compiler-dependent) printed as 1.234e+1
or 1.234e+01 or 1.234e+001. Since I know the expected number of digits
for the exponent, I would like to control it. I can control the width
of the base part but not the exponent. Does anybody know how to set the
width of the exponent?

TIA.

Allen
 
V

Victor Bazarov

I use cout to print numbers in scientific notation. I noticed that a
number like 12.34 is sometimes (compiler-dependent) printed as 1.234e+1

No, it's not compiler-dependent. It should always be printed as 1.234e+01
unless the library is not standard. Let me take it back. It is compiler-
dependent in the sense that some compilers are supplied with libraries
which are not Standard-compliant.
or 1.234e+01 or 1.234e+001. Since I know the expected number of digits
for the exponent, I would like to control it. I can control the width
of the base part but not the exponent. Does anybody know how to set the
width of the exponent?

There is no standard way. The Standard only says something like "as many
digits as necessary but no fewer than 2 digits". Not all libraries follow
that rule, though. If you want to sometimes print only one digit, then
you will have to remove the leading zeros after 'e' yourself.

V
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top