Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf format issue

Status
Not open for further replies.

Maatmes

Programmer
Jun 8, 2006
1
US
I dont know if I'm just having a memory glitch, but the printf format for float is not working as I expect it to.

This example:

#include <stdio.h>

int main()
{
float value;

for(value = 9.990 ;value <= 10.01;value += 0.001)
printf("%02.04f\n", value);
return(0);
}
produces the following output

9.9900
9.9910
9.9920
9.9930
9.9940
9.9950
9.9960
9.9970
9.9980
9.9990
10.0000
10.0010
10.0020
10.0030
10.0040
10.0050
10.0060
10.0070
10.0080
10.0090

The problem is I want to generate a fixed field file so I need an extra digit in the 9.xxxx values. The %02 should give me a minimum field of 2 and I would expect the first value to be 09.9900. I understand that the precesion .04 simply ignores the 0 , but I've been trying everything I can think of. I'm using gcc 3.3.5 on linux and I get the same problem in Dev-C++.

What am I forgetting?
 
Your printf needs to be:

Code:
printf("%07.04f\n",value);

The width specifier specifies the width of the whole thing, not just what's to the left of the decimal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top