Whats the difference?
#1

What does it do different if you put "%f" rather than "%.2f" ??

Thanks for any help,

- Josh
Reply
#2

%.2f specifies the precision of the float to print out to 2. This would print out a float to 2 decimal places:

pawn Код:
// ..
printf( "%.2f", 3.141592 );
//Would output 3.14, because precision is 2 places.

// ..
printf( "%f", 3.141592 );
//Would output 3.141592, because there is no precision set.

// ..
printf( "%.4f", 3.141592 );
//Would output 3.1415, because precision is 4 places.
For more info, see this article I wrote on the wiki about floats:

https://sampwiki.blast.hk/wiki/Floats

and more specifically;
https://sampwiki.blast.hk/wiki/Floats#Float_-.3E_String
Reply
#3

try this and find out yourself:

pawn Код:
public OnPlayerSpawn(playerid)
{
     new
          Float: gHP,
          string[32]
     ;
     GetPlayerHealth(playerid, gHP);
     format(string, sizeof(string), "HP: %f %.2f", gHP, gHP);
     SendClientMessage(playerid, ~1, string);
     return 1;
}
if your hp is 99,5949123 '%f' will print you 99,5949... otherwise %.2f will print you 99,59.

^ posted in the same time, heehe
Reply
#4

%f is the default Float showing by 6 numbers if i'm not wrong and %.2f shows only 2 numbers after the coma. E.g

%f = 123.1234
%.2 = 1.12

//Edit:

Someone was faster than me :/
Reply
#5

Quote:
Originally Posted by blewert
Посмотреть сообщение
%.2f specifies the precision of the float to print out to 2. This would print out a float to 2 decimal places:

pawn Код:
// ..
printf( "%.2f", 3.141592 );
//Would output 3.14, because precision is 2 places.

// ..
printf( "%f", 3.141592 );
//Would output 3.141592, because there is no precision set.

// ..
printf( "%.4f", 3.141592 );
//Would output 3.1415, because precision is 4 places.
For more info, see this article I wrote on the wiki about floats:

https://sampwiki.blast.hk/wiki/Floats

and more specifically;
https://sampwiki.blast.hk/wiki/Floats#Float_-.3E_String
So basically, it wouldnt matter whether you put .2f, it just makes it look neater?
Reply
#6

Quote:
Originally Posted by Joshb93
Посмотреть сообщение
So basically, it wouldnt matter whether you put .2f, it just makes it look neater?
If you want to output a player health so i don't want to print 3.141592 HP but 3.1 or 3 which you do %.0f to print "3"
Reply
#7

Quote:
Originally Posted by Joshb93
Посмотреть сообщение
So basically, it wouldnt matter whether you put .2f, it just makes it look neater?
Essentially, for printing out stuff; yes. It makes long floats like 3.141592 look like 3.14.

But for using with actual functions that use floats as arguments (Such as SetPlayerPos), then no - it decreases the accuracy of the float - because it essentially rounds up the float to a certain number of decimal places.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)