printf, padding integral double values

  • Thread starter Daniel Molina Wegener
  • Start date
D

Daniel Molina Wegener

Hello,

How can I pad integral values on double variables with
printf.

I know that %.nf or %.ng sets the precision. But I want to
pad the integral value with zeroes, like 001.023 or 032.012.

Any way to do this with printf?.

Regards.
 
M

Michael Mair

Daniel said:
Hello,

How can I pad integral values on double variables with
printf.

I know that %.nf or %.ng sets the precision. But I want to
pad the integral value with zeroes, like 001.023 or 032.012.

Any way to do this with printf?.

Regards.

#include <stdio.h>

int main (void)
{
printf("%0*.*f\n", 6+1, 3, 1.23);
return 0;
}

Cheers
Michael
 
M

Martin Ambuhl

Daniel said:
Hello,

How can I pad integral values on double variables with
printf.

I know that %.nf or %.ng sets the precision. But I want to
pad the integral value with zeroes, like 001.023 or 032.012.

Any way to do this with printf?.

#include <stdio.h>

int main(void)
{
double x[] = { 1.023, 32.012 };
size_t i;

printf("[output]\n");
for (i = 0; i < sizeof x / sizeof *x; i++)
printf("%%g: %g, %%f %f, %%.3f %.3f, %%07.3f %07.3f\n",
x, x, x, x);
return 0;
}

[output]
%g: 1.023, %f 1.023000, %.3f 1.023, %07.3f 001.023
%g: 32.012, %f 32.012000, %.3f 32.012, %07.3f 032.012
 

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top