Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

OklyDokly

printf problem

This topic is 5300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!