file formating...help needed

C

comp_novice

I need to control the number of digits to be printed after the
exponential. Please have a look at the example below:

float fVal = 0.06854344

The printf that I am using is
fprintf(pfile, "%E\n", fVal);

The result is 6.854344E-002

The output that I require should be something like this 6.854344E-02

There is an extra zero in the output that I get. I need to trim that.

How do I accomplish this?

Thanks in advance...
 
A

Adam Warner

I need to control the number of digits to be printed after the
exponential. Please have a look at the example below:

float fVal = 0.06854344

The printf that I am using is
fprintf(pfile, "%E\n", fVal);

The result is 6.854344E-002

The output that I require should be something like this 6.854344E-02

Your C environment appears to be buggy. Can you update it? According to
the C99 standard (7.19.6.1):

The E conversion specifier produces a number with E instead of e
introducing the exponent. The exponent always contains at least two
digits, and only as many more digits as necessary to represent the
exponent. If the value is zero, the exponent is zero.

If you can't fix your environment it looks like you'll have to
post-process the "printed" output as a string.

Regards,
Adam
 
M

Martin Ambuhl

comp_novice said:
I need to control the number of digits to be printed after the
exponential. Please have a look at the example below:

float fVal = 0.06854344

The printf that I am using is
fprintf(pfile, "%E\n", fVal);

The result is 6.854344E-002

The output that I require should be something like this 6.854344E-02

There is an extra zero in the output that I get. I need to trim that.

How do I accomplish this?

1) By switching to a C99-aware library. C89 & C90 had this text:
The exponent always contains at least two digits. If the value is
zero, the exponent is zero.

The C99 standard changed this to:
The exponent always contains at least two digits, and only as
many more digits as necessary to represent the exponent. If the
value is zero, the exponent is zero.

2) If (1) is impractical, simply sprintf() to a string and edit the
resulting string before printing. A function to do this is trivial.
 
M

Martin Ambuhl

Adam said:
Your C environment appears to be buggy.

It is only "buggy" if you think that conforming to the C89 & C99
standard but not observing the C99 changes is "buggy."
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top