Core Function Fmt
(Created page with "<pre> Fmt( <format control>, <params> ) </pre> === Description === Returns a formatted string (similar to the C sprintf() function). === Parameters === ==== format control ==...") |
m (1 revision) |
||
(31 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | Fmt( < | + | Fmt( <expression>, <expressions>... ) |
</pre> | </pre> | ||
=== Description === | === Description === | ||
− | + | Create a formatted string. | |
− | === | + | === expression === |
− | + | The format string and flags to use (see Remarks). | |
− | + | ==== expressions ==== | |
− | + | Variables that will be output according to the "Format Control". | |
− | + | ||
− | Variables that will be output according to the " | + | |
=== Return Value === | === Return Value === | ||
− | Success: | + | Success: Returns the formatted string. |
− | Failure: Returns | + | Failure: Returns an empty string. |
=== Remarks === | === Remarks === | ||
− | + | If you are looking for something that works similar to C's sprintf() function see this [[Core Function SPrintf|SPrintf( <format control>, <params> )]] | |
− | If you | + | Warning: If you expect to use int, byte, float etc in the params then you must cast it as such for example: |
− | " | + | <syntaxhighlight lang="sputnik"> |
+ | $myint = 100; | ||
+ | $val = fmt("Value is 0x\{0:x}", (int)$myint); // The (int) will only take in $myint | ||
+ | println($val); | ||
− | + | $myint = 100; | |
+ | $myint2 = 100; | ||
+ | $val = fmt("Value is 0x\{0:x}", (int)($myint + $myint2)); // The (int) will now take in $myint AND $myint2 | ||
+ | println($val); | ||
+ | </syntaxhighlight> | ||
− | + | Note how $myint was cast as an int? using (int)$myint this is vital since if you dont do that it might send a string or a float you never really know with $variables so its best to cast it. | |
− | + | There is an alternative method example: | |
− | + | <syntaxhighlight lang="sputnik"> | |
+ | $myint = 100; | ||
+ | $val = fmt("Value is 0x\{0:x}", int($myint)); | ||
+ | </syntaxhighlight> | ||
− | + | Its up to you to decide which one to use. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | Go see [[Core Function PrintfC|PrintfC( <expression>, <expressions>... )]] for details on the format string | ||
+ | Anything thats valid for PrintfC to print to console window is also valid for Fmt to use to create strings. | ||
=== Example === | === Example === | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $ | + | $val = fmt("Value is '0x\{0:x}'", (int)100); |
− | + | println($val); // Prints 0x64 since the {0:x} is telling it to place param 0 as :x meaning hex | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | println($ | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:37, 14 June 2015
Fmt( <expression>, <expressions>... )
Contents |
Description
Create a formatted string.
expression
The format string and flags to use (see Remarks).
expressions
Variables that will be output according to the "Format Control".
Return Value
Success: Returns the formatted string.
Failure: Returns an empty string.
Remarks
If you are looking for something that works similar to C's sprintf() function see this SPrintf( <format control>, <params> )
Warning: If you expect to use int, byte, float etc in the params then you must cast it as such for example:
$myint = 100; $val = fmt("Value is 0x\{0:x}", (int)$myint); // The (int) will only take in $myint println($val); $myint = 100; $myint2 = 100; $val = fmt("Value is 0x\{0:x}", (int)($myint + $myint2)); // The (int) will now take in $myint AND $myint2 println($val);
Note how $myint was cast as an int? using (int)$myint this is vital since if you dont do that it might send a string or a float you never really know with $variables so its best to cast it.
There is an alternative method example:
$myint = 100; $val = fmt("Value is 0x\{0:x}", int($myint));
Its up to you to decide which one to use.
Go see PrintfC( <expression>, <expressions>... ) for details on the format string
Anything thats valid for PrintfC to print to console window is also valid for Fmt to use to create strings.
Example
$val = fmt("Value is '0x\{0:x}'", (int)100); println($val); // Prints 0x64 since the {0:x} is telling it to place param 0 as :x meaning hex