Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf, padding integral double values

Reply
Thread Tools

printf, padding integral double values

 
 
Daniel Molina Wegener
Guest
Posts: n/a
 
      02-14-2005

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.
--
. 0 . | Daniel Molina Wegener
. . 0 | dmw at trauco dot cl
0 0 0 | http://trauco.cl/
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      02-14-2005


Daniel Molina Wegener wrote:
> 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
--
E-Mail: Mine is a gmx dot de address.

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-14-2005
Daniel Molina Wegener wrote:
> 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[i], x[i], x[i], x[i]);
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


--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: istream read failure: integral values as bool Abhishek Padmanabh C++ 1 03-21-2011 04:17 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
integral (non char) types allow padding? Mihai Rusu C++ 1 08-08-2008 09:27 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57