printf problem

Started by
5 comments, last by OklyDokly 20 years ago
Hi, I''m having real problems getting printf to work the way I want to. I want to display for example 4.24f as '' 4.24'' in the console, preserving the spacing but all of: printf ( "% 4.2f", 4.24f ); and printf ("%4.2f", 4.24f ); and printf ("%04.2f", 4.2f ); All display the same result which is: ''4.24'' The width seems to be ignored, as well as the custom spacing I want to put in. Does anyone understand why, any help will be appreciated.
Advertisement
printf(" %04.2f", 4.2f );

Is what you want.
lol that was just an example

I actually want to format a floating point number, so it always appears the same length on screen

so lets say:

float f = 1.3f;

then I hoped that:
printf( "% 5.2f", f );

would display:
'' 1.30''
( with 2 spaces )

and not
''1.30''

and if f = 11.3f;
then I want it to display
'' 11.30''
( with one space )

and not
''11.30''

hmm hope that explains it a bit better.

consider using sprintf and c-string.

I forget the format, so I can't post an example.

edit:
Are you kidding? This works just fine:

float ee = 5.203f;
printf( "% 10.2f\n", ee );
printf( "% 10.2f\n", 500.2f );
printf( "% 10.2f\n", 23.21f );
printf( "% 10.2f\n", 90.593f );

Output:
      5.20    500.20     23.21     90.59  

Remember, the format is: % [zero/space] [minimum width ] . [precision] [type]

[edited by - alnite on April 15, 2004 7:34:10 PM]
It looks like you don''t quite understand how printf works.

You''re putting an actual number after the % which you don''t need. If you want to display '' 4.24f'' (with a leading space, if I''m correct), then you use:
float f = 4.24f;
printf( " %.2f", f );
Notice that the printf doesn''t actually have the number 4.24 in it; it only has ".2" indicating that it should print out only the first two digits after the decimal point. And you put the percent right next to it; don''t put spaces in between. Put spaces before and after. It won''t interfere.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
quote:Original post by Promit
It looks like you don''t quite understand how printf works.

You''re putting an actual number after the % which you don''t need. If you want to display '' 4.24f'' (with a leading space, if I''m correct), then you use:
float f = 4.24f;
printf( " %.2f", f );
Notice that the printf doesn''t actually have the number 4.24 in it; it only has ".2" indicating that it should print out only the first two digits after the decimal point. And you put the percent right next to it; don''t put spaces in between. Put spaces before and after. It won''t interfere.
Actually, I think what he wants to do is to right align the number.
Thanks for your help alnite.

Kind of weird actually, when I take the number up from 4.2 to 5.2 or above it actually seems to work fine.

However I wasn''t really concentrating last night and the only problem is that the number 0 seems to be aligned as

''0.00''

instead of

'' 0.00''

as I want it when I put the number 4.2 in.

Anyway it seems to be fixed for what I want to do.

This topic is closed to new replies.

Advertisement