473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

printf, padding integral double values


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/
Nov 14 '05 #1
2 2137


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.

Nov 14 '05 #2
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
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Piotr B. | last post by:
Hello, I use MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP. I tried to output a "long double" variable using stdio printf(). I've tried various %formats (%llf, %Lf etc.), but none of them...
4
by: Piotr B. | last post by:
Hello, Here are two questions on formatting values using printf(): 1. How to strip trailing zeroes from float values? printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000", while I want to...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
2
by: David Mathog | last post by:
Is there some method for restricting the maximum number of characters that will be emitted by a printf() function? Where printf() here is generic and refers to sprintf, fprintf, etc. Here's an...
15
by: Michael B Allen | last post by:
I'm printing tables of structure members in a terminal and I would like to use printf with an arbitrary pointer so that the code is smaller and I don't need to switch over ever possible type. ...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
13
by: aarklon | last post by:
Hi all, arrays are guaranteed to be contiguous with no padding before or after any array member , but what about enums ..???
2
by: Mihai Rusu | last post by:
Hello I am trying to figure out if the standard allows integral type representations with padding (ie value representation != object representation). A conforming implementation can have: - a...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.