[comp.lang.c] printf format

root@ozdaltx.UUCP (root) (12/13/89)

In the back of my mind, it seems like I read that there is a format
character used in printf(C) that allows the formatting of numbers.
I'd like to take a number, ie. 123456.78 and print it as
123,456.78 .  If this info is in the manual (SCO), I can't find it or
it is not written clearly enough to say that what it does.

If printf does not have this ability, it should be added.  

thanks in advance....
Scotty
{ames,rutgers,texsun,smu}!attctc!ozdaltx!sysop 

karl@haddock.ima.isc.com (Karl Heuer) (12/14/89)

In article <5761@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>[Can printf() format numbers with embedded commas?]

Not in K&R or ANSI C, though it's conceivable that some vendor has added it as
an extension.

You may be thinking of the localization features of ANSI C.  These provide
some support for determining what the local convention is (e.g. where to place
the commas, what the currency symbol looks like, etc.), but there's no library
function that actually makes use of any this information (except for the
decimal-point character).

>If printf does not have this ability, it should be added.

I think a better idea is to have a separate function to do this type of
formatting, then print it with %s.  (Just like you have to do with a time_t,
for example, to make it print in a human-readable format.)  I suspect this is
what the Committee had in mind.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

javiv@nsi.UUCP (Javier Vilarroig Christensen.) (12/20/89)

In article <5761@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>In the back of my mind, it seems like I read that there is a format
>character used in printf(C) that allows the formatting of numbers.
[lines deleted]
>If printf does not have this ability, it should be added.  
>
>thanks in advance....
>Scotty

It's imposible to do this with printf. You must make a funtion to do it in
a string, and then feed it to printf.





-- 
+------------------------------------+-------------------------------------+
| Javier Vilarroig Christensen       | PHONE: 34 3 210-33-55 (VOICE)       |
| NEXUS Servicios de Informacion S.L.|        34 3 214-72-62 (DATA)        |
+------------------------------------+-------------------------------------+
| SMAIL: Travesera de Dalt 104 Ent. 5| EUNET: javiv@nsi.es                 |
|        08024 - Barcelona - Spain   |        javiv@nexus.nsi.es           |
+------------------------------------+-------------------------------------+

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (12/21/89)

In article <987@nsi.UUCP> javiv@nsi.UUCP (Javier Vilarroig Christensen.) writes:
| In article <5761@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
| >In the back of my mind, it seems like I read that there is a format
| >character used in printf(C) that allows the formatting of numbers.
| 
| It's imposible to do this with printf. You must make a funtion to do it in
| a string, and then feed it to printf.

  Since I can't read the original poster's mind, I'll take a stab that
he *might* be thinking of the * character, used to specify the width in
an argument evaluated at runtime.

Ex:	n = 14; 		/* set field width */
	printf("Val: %*d sec", n, m);
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

scott@bbxsda.UUCP (Scott Amspoker) (12/21/89)

In article <1940@crdos1.crd.ge.COM> davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes:
>  Since I can't read the original poster's mind, I'll take a stab that
>he *might* be thinking of the * character, used to specify the width in
>an argument evaluated at runtime.
>
>Ex:	n = 14; 		/* set field width */
>	printf("Val: %*d sec", n, m);

No, the original, original poster wanted full numeric formatting capabilities
(you know, comma insertion and so forth).  That's all I need - a full numeric
formatter linked in everytime I use printf().



-- 
Scott Amspoker
Basis International, Albuquerque, NM
(505) 345-5232
unmvax.cs.unm.edu!bbx!bbxsda!scott

gaynor@busboys.rutgers.edu (Silver) (12/21/89)

Adding numeric formatting to printf is not difficult, once the method behind
printf's madness is divined (there was more madness than method in the printf I
played with).  Added cute things like the comma ability, filling with a
character other than blank, dollar signs, arbitrary radixes < z, etc.  Don't
ask me for the code, I don't have access to it, sorry.  I'd hunt down a pd
printf (uunet.uu.net, probably) and redo it, but I'm a little busy at the
moment.

Regards, [Ag]

jpr@dasys1.UUCP (Jean-Pierre Radley) (12/24/89)

In article <5761@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>In the back of my mind, it seems like I read that there is a format
>character used in printf(C) that allows the formatting of numbers.
>I'd like to take a number, ie. 123456.78 and print it as
>123,456.78 . 
>If printf does not have this ability, it should be added.  

And make allowances for European conventions too, eh?

In the meantime, while working on a General Ledger program which used
'awk' for formatting, I found the same limitation in the 'printf'
function of 'awk'. So I wrote a sed filter to insert my commas:

sed -f comma.sed <infile >outfile

where comma.sed is:


# comma.sed, inserts commas in numbers preceded by enough blanks
: R
/[0-9][0-9][0-9[0-9][,.]/s/ \([-+0-9]*[0-9]\)\([0-9][0-9][0-9][,.]\)/\1,\2/g
t R

I noted, along the way, that a sed script allows ONE commented line at the
outset.
-- 
Jean-Pierre Radley					      jpr@jpradley.uucp
New York, NY					      72160.1341@compuserve.com

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (12/27/89)

  One way to do any unsupported format is to implement a procedure which
does what you want and returns a pointer to char which you can use with
the %s format. If you want to call it more than once as an argument to a
single printf call you have to pass in a buffer. Other wise you can use
an internal buffer.
-- 
	bill davidsen - sysop *IX BBS and Public Access UNIX
davidsen@sixhub.uucp		...!uunet!crdgw1!sixhub!davidsen

"Getting old is bad, but it beats the hell out of the alternative" -anon

bill@twwells.com (T. William Wells) (12/29/89)

In article <11443@dasys1.UUCP> jpr@dasys1.UUCP (Jean-Pierre Radley) writes:
: I noted, along the way, that a sed script allows ONE commented line at the
: outset.

try:

	sed -e cmd1 -e cmd2 ....

or:

	sed 'cmd1
	     cmd2'

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com