tags:

views:

59

answers:

2

Using fprintf, what is the format-string to output two digits after a decimal separator?

float number = 3.0;
fprintf(pFile, "%10.02f", number); 

This always outputs 3 instead of 3.00

How do I fill up the digits after decimal separator with zeros? I want 3.00 written in the output file.

+4  A: 

Use %.2f as your format specifier instead, or if you want 10 spaces first %10.2f

Brian R. Bondy
A: 

I suspect you want to represent currency amounts. Please don't use floats for this purpose. Decimal numbers aren't represented exactly with floats, this can lead to roundoff errors you don't want.

Alexandre C.