printf() formatting - stripping zeroes, padding

P

Piotr B.

Hello,

Here are two questions on formatting values using printf():

1. How to strip trailing zeroes from float values?
printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000",
while I want to have "1.777 1.2", without loosing 6-digit
precision.

2. Is it possible to pad values with other characters than
space and zero? printf(">%5s<", "abc") displays "> abc<".
I would like to have e.g. ">##abc<" or ">--abc<".

Thank you!
 
V

Victor Bazarov

Piotr said:
Here are two questions on formatting values using printf():

1. How to strip trailing zeroes from float values?
printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000",
while I want to have "1.777 1.2", without loosing 6-digit
precision.

Use %g.
2. Is it possible to pad values with other characters than
space and zero? printf(">%5s<", "abc") displays "> abc<".
I would like to have e.g. ">##abc<" or ">--abc<".

Not for outputting strings. Create a temporary string and do
your own padding.

V
 
C

CBFalconer

Piotr B. said:
1. How to strip trailing zeroes from float values?
printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000",
while I want to have "1.777 1.2", without loosing 6-digit
precision.

printf("%.3f %.2f", 1.777, 1.2);

Read the standard. Don't cross post between c.l.c and c.l.c++;
they are different languages. Follow-ups set. c.l.c++ probably
won't use printf anyhow.

--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan"
"I listened to Toronto come back from 3:0 in '42, I plan to
watch Boston come back from 3:0 in 04"
 
V

Victor Bazarov

CBFalconer said:
printf("%.3f %.2f", 1.777, 1.2);

%.2f will still print one trailing 0 after 1.2, so you probably
meant

printf(%.3f %.1f", 1.777, 1.2);
Read the standard. Don't cross post between c.l.c and c.l.c++;
they are different languages.

They have a lot in common. Up until the new C Standard (come
in 1999, IIRC) they shared the same standard library.
> Follow-ups set. c.l.c++ probably
won't use printf anyhow.

Just to let you know, 'printf' is just as much part of C++ as it
is of C circa 1998. Whether to use any particular part of the
Standard library is, of course, up to the programmer.

Be well!
 
P

Piotr B.

Victor said:
> %.2f will still print one trailing 0 after 1.2, so you probably
> meant
> printf(%.3f %.1f", 1.777, 1.2);

I'm afraid this won't work, because it cuts down precision
> I want to have "1.777 1.2", without loosing 6-digit precision.

But Victor has already solved this one - I should use %g. Thanks once more.
> won't use printf anyhow.
printf() and scanf() work about 5 times faster than cin >> and cout <<
(MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP).
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top