Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf() formatting - stripping zeroes, padding

Reply
Thread Tools

printf() formatting - stripping zeroes, padding

 
 
Piotr B.
Guest
Posts: n/a
 
      10-19-2004
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!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-19-2004
Piotr B. wrote:
> 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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-19-2004
"Piotr B." wrote:
>
> 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"


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-19-2004
CBFalconer wrote:
> "Piotr B." wrote:
>
>>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);


%.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!
 
Reply With Quote
 
Piotr B.
Guest
Posts: n/a
 
      10-20-2004
Victor Bazarov wrote:
> %.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
to 3 and 1 digits. In my original post, I wrote:
> 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.

CBFalconer wrote:
> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf() formatting - stripping zeroes, padding Piotr B. C++ 2 10-19-2004 07:29 PM
printf() formatting - stripping zeroes, padding Michal Prinke C Programming 0 10-19-2004 03:28 PM
printf() formatting - stripping zeroes, padding Michal Prinke C++ 0 10-19-2004 03:28 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM
regex for stripping HTML Michael Vilain Perl 4 10-30-2003 01:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57