Format

From SA-MP Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 20:08, 31 October 2009
Amit B (Talk | contribs)
(Added second example)
← Previous diff
Revision as of 12:44, 1 November 2009
Dark Kostas (Talk | contribs)
(format(result,sizeof(result), "This is a string containing the number %i.",number);<--- You cant show the string by just typing. You need %s)
Next diff →
Line 20: Line 20:
format(result,sizeof(result), "The number is %i.",number); //-> The number is 42. format(result,sizeof(result), "The number is %i.",number); //-> The number is 42.
new string[]= "simple message"; new string[]= "simple message";
-format(result,sizeof(result), "This is a string containing the number %i.",number);+format(result,sizeof(result), "This is a %s containing the number %i.",string, number);
// This is a simple message containing the number 42. // This is a simple message containing the number 42.
</pawn> </pawn>

Revision as of 12:44, 1 November 2009

Format


Formats a string to include variables and other strings inside it.


Parameters:
(output[], len, const format[], {Float,_}:...)
output[]The string to output the result to
lenThe maximum length output can contain
format[]The format string
{Float,_}:...Indefinite number of arguments of any tag


This function does not return a specific value, it's best to simply ignore it.


Format Strings

Placeholder Meaning
%b Inserts a number at this position in binary radix
%c Inserts a single character.
%d Inserts an integer (whole) number
%f Inserts a floating point number.
%i Inserts an integer.
%s Inserts a string.
%x Inserts a number in hexadecimal notation.
%% Inserts the literal '%'

The values for the placeholders follow in the exact same order as parameters in the call.

You may optionally put a number between the '%' and the letter of the placeholder code. This number indicates the field width; if the size of the parameter to print at the position of the placeholder is smaller than the field width, the field is expanded with spaces.

new result[128];
new number = 42;
format(result,sizeof(result), "The number is %i.",number);  //-> The number is 42.
new string[]= "simple message";
format(result,sizeof(result), "This is a %s containing the number %i.",string, number);
// This is a simple message containing the number 42.
new string[64];
format(string,sizeof(string),"Your score is: %d",GetPlayerScore(playerid));
SendClientMessage(playerid,0xFFFFFFAA,string);

Related Functions

The following functions might be useful as well, as they're related to this function in one way or another.

  • Print: Print a basic message in the logs and console.
  • Printf: Print a formatted message into the logs and console.