By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,004 Members | 2,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,004 IT Pros & Developers. It's quick & easy.

formatting question

P: n/a
Hello All,

I have a following piece of code in C that I need to rewrite to C++.
However I just don't seem to be able to understand the format rules in
C.

The C code look like this:

I have two doubles x and y which has a value. Now their values are
written to
output with printf in the following way.

printf("%1f %3.2e\n",x,y);

I have read in a book that for instance %5.4f means write the float
with 5 digit
widt where 4 digit are dedicated to the decimal portion. However why
can I then
print out values with more then one digit ????
Can anyone explain this to me ????
And then what does %3.2e means then ??
That I will get at most 3 digits ???
where 2 are dedicated to the decimal ???
How can I rewrite this to C++ using cout to write to output ??
Thank you in advance

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
wo*********@yahoo.com wrote:
I have a following piece of code in C that I need to rewrite to C++.
However I just don't seem to be able to understand the format rules in
C.

The C code look like this:

I have two doubles x and y which has a value. Now their values are
written to
output with printf in the following way.

printf("%1f %3.2e\n",x,y);

I have read in a book that for instance %5.4f means write the float
with 5 digit
widt where 4 digit are dedicated to the decimal portion.
Kind of. 5 would be the width of the whole field and 4 is the decimal
digits after the dot. If the number doesn't fit in 5 symbols considering
leading digits (before the decimal point) and the sign and the dot itself,
then the width is ignored.
However why
can I then
print out values with more then one digit ????
Because the width is ignored if the number doesn't fit.
Can anyone explain this to me ????
Get a good book.
And then what does %3.2e means then ??
It means to print out in scientific format with two decimal digits after
the floating point. The width 3 is not going to be enough, so it will be
ignored.
That I will get at most 3 digits ???
No. You will get 2 digits after the dot.
where 2 are dedicated to the decimal ???
Yes.
How can I rewrite this to C++ using cout to write to output ??


Read about 'fixed' and 'scientific' manipulators and also 'setprecision'
and 'setw', although the latter is not that important.

V
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.