[comp.lang.c] Padding floats with zeros in printf??

robert@nereid.jpl.nasa.gov (Robert Angelino) (04/06/91)

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

I want something like

	01.132

The book "C A Reference Manual" by Steele & Harbison say that the
following should work:
	printf("%02.3f\n",1.1324); OR
	printf("%#02.3f\n",1.1324); OR
	printf("%02.3g\n",1.1324); OR
	printf("%#02.3g\n",1.1324);

Now, I've tried all of these (including the "hammer") and nothing seems
to work.  If the book says it should work then the compiler is not doing
it's job correctly.  Does anyone know how to get around this using this
compiler???

Please e-mail directly.

thanks in advance


-- 
    -     ------       -                              Robert Angelino
   | |   | ----  \    | |                             ms T-1704L
   | |   | |   \ |    | |                             4800 Oak Grove Drive
   | |   | | --  |    | |                             Pasadena, CA 91109
---| |   | | \__/     | |___                          robert@triton.jpl.nasa.gov
\____|et |_|ropulsion |_____\aboratory                (818) 354-9574

harry@dustbin.uk.sun.com (Harry Protoolis - Sun EHQ) (04/06/91)

In article <6158@mahendo.Jpl.Nasa.Gov>, robert@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@uk.sun.com
(smart JANET mailers) harry.protoolis@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