zero pad a double

I

Ian T

Hi

Does anyone know how to zero-pad a float or double? I know %02d works
for ints but it doesn't work for doubles e.g. 1.2 -> 01.2

Thanks

Ian
 
R

Richard Heathfield

Ian T said:
Hi

Does anyone know how to zero-pad a float or double? I know %02d works
for ints but it doesn't work for doubles e.g. 1.2 -> 01.2

It does if you do it right:

$ cat foo.c
#include <stdio.h>
#include <math.h>

int main(void)
{
double d = 1.2;
printf("%04.1f\n", d);
return 0;
}

$ ./foo
01.2

The figure just before the dot separator gives the total width of the
field, including the radix point and any post-radix digits.
 

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,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top