Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting output as :
-2.28463E+001,5.71299E+001,3.14288E+001,4.57282E+001,4.54909E+001

I need output as:
-2.28463E+01,5.71299E+01,3.14288E+01,4.57282E+01,4.54909E+01

Something is wrong with formating the string.
C++
for (count1=0;count1<Header.num_series;count1++)
{
   fprintf(output_stream,"%.5E",data[count1]);
			
   if(count1!=Header.num_series-1) fprintf(output_stream,",");
}

Please help me out.
Thank you.
Posted
Updated 14-Jun-13 9:37am
v2
Comments
joshrduncan2012 14-Jun-13 15:22pm    
What do you not like about what you are getting right now? Too many leading zeros?

In would appear that on Windows at least the Visual C Runtime MSVCRT doesn't offer control over the number of exponent digits that are output. Even my version of the same algorithm doesn't provide control over this at the moment. It is hard coded to be 3.

This is an extract from my version which produces the same output as Microsoft's but the code can actually be read and debugged. :-)

buffer[ 2 ] = ( exp % 10 ) + '0';
exp = exp / 10;
buffer[ 1 ] = ( exp % 10 ) + '0';
exp = exp / 10;
buffer[ 0 ] = ( exp % 10 ) + '0';
buffer += 3;


I'm afraid you're out of luck unless you want to code your own custom fprintf which I can tell you from experience is a lot of work.

[modified]

It would appear that I'm out of date and MS have improved there formatter implementation. See André's Solution 2.

That's good because it was rubbish before but bad because now it's better than mine which means I have even more work to do.
 
Share this answer
 
v2
Looks like you can use the _set_output_format[^] function with _TWO_DIGIT_EXPONENT as the parameter to specify that two digits are to be used in the exponent.

Off course if more digit are needed to express the number they are used; as demonstrated in the example from the link.
 
Share this answer
 
If you are using Windows, have a look at _set_output_format[^] function (see the sample code).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900