SA-MP Forums Archive
Whats the difference? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Whats the difference? (/showthread.php?tid=299578)



Whats the difference? - Joshb93 - 26.11.2011

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

Thanks for any help,

- Josh


Re: Whats the difference? - blewert - 26.11.2011

%.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


Re: Whats the difference? - fordawinzz - 26.11.2011

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


Re: Whats the difference? - TheArcher - 26.11.2011

%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 :/


Re: Whats the difference? - Joshb93 - 26.11.2011

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?


Re: Whats the difference? - TheArcher - 26.11.2011

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"


Re: Whats the difference? - blewert - 26.11.2011

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.