Padding floats with zeros in printf??

Harry Protoolis - Sun EHQ harry at dustbin.uk.sun.com
Sat Apr 6 23:29:23 AEST 1991


In article <6158 at mahendo.Jpl.Nasa.Gov>, robert at nereid.jpl.nasa.gov (Robert Angelino) writes:
|> background: I'm running SunOS 4.1 on a SPARC using the Sun
|> 	    C compiler.
|> 
|> I'm doing something like so:
|> 
|> 	printf("%02.3f\n",1.1324);
|> 
|> and I'm getting this:
|> 
|> 	1.132

Try printf("%06.3f\n", 1.1324);

The first part of the size specifier is a *field width*, so you
need to allow for the whole output string (ie 2 (digits left of d.p) + 1
(decimal point) + 3 (digits right of d.p) = 6).

Hope this helps,
Harry
-- 
(smart Internet mailers) harry.protoolis at uk.sun.com
(smart JANET mailers) harry.protoolis at sun-microsystems.co.uk
(dumb uucp mailers) ...!sun!sunuk!harry.protoolis

'When I give food to the poor they call me a saint.
 When I ask why the poor have no food they call me a communist.'
         - Dom Helder Camara



More information about the Comp.lang.c mailing list