Core Function Fmt

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(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( <format control>, <params> )
+
Fmt( <expression>, <expressions>... )
 
</pre>
 
</pre>
  
 
=== Description ===
 
=== Description ===
  
Returns a formatted string (similar to the C sprintf() function).
+
Create a formatted string.
  
=== Parameters ===
+
=== expression ===
  
==== format control ====
+
The format string and flags to use (see Remarks).
  
The format and flags to use (see Remarks).
+
==== expressions ====
  
==== params ====
+
Variables that will be output according to the "Format Control".
 
+
Variables that will be output according to the "format control".
+
  
 
=== Return Value ===
 
=== Return Value ===
  
Success: Return the formatted string according to "variable format" defined in the "format control" parameter.
+
Success: Returns the formatted string.  
  
Failure: Returns "" (blank string).  
+
Failure: Returns an empty string.
  
 
=== Remarks ===
 
=== Remarks ===
  
To prevent buffer overflow, each "variable" is limited to 65535 characters.
+
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 want to have "%" in the format control you must enter "%%".
+
Warning: If you expect to use int, byte, float etc in the params then you must cast it as such for example:
  
"variable format" is; %[flags] [width] [.precision] type
+
<syntaxhighlight lang="sputnik">
 +
$myint = 100;
 +
$val = fmt("Value is 0x\{0:x}", (int)$myint); // The (int) will only take in $myint
 +
println($val);
  
        '''Width Specification'''
+
$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>
  
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
+
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.
  
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
+
There is an alternative method example:
  
        '''Type Specification'''
+
<syntaxhighlight lang="sputnik">
 +
$myint = 100;
 +
$val = fmt("Value is 0x\{0:x}", int($myint));
 +
</syntaxhighlight>
  
<pre>
+
Its up to you to decide which one to use.
Type        Variable type          Output format
+
d, i        Integer                Signed decimal integer.
+
o          Integer                Unsigned octal integer.
+
u          Integer                Unsigned decimal integer.
+
x          Integer                Unsigned hexadecimal integer, using "abcdef".
+
X          Integer                Unsigned hexadecimal integer, using "ABCDEF".
+
e          Float                  Signed value having the form [ - ]d.dddd e [sign]ddd where d is a single decimal
+
                                    digit, dddd is one or more decimal digits, ddd is exactly three decimal digits,
+
                                    and sign is + or -.
+
E          Float                  Identical to the e format except that E rather than e introduces the exponent.
+
f          Float                  Signed value having the form [ - ]dddd.dddd, where dddd is one or more decimal digits.
+
                                    The number of digits before the decimal point depends on the magnitude of the number,
+
                                    and the number of digits after the decimal point depends on the requested precision.
+
g          Float                  Signed value printed in f or e format, whichever is more compact for the given value
+
                                    and precision. The e format is used only when the exponent of the value is less
+
                                    than -4 or greater than or equal to the precision argument. Trailing zeros are
+
                                    truncated, and the decimal point appears only if one or more digits follow it.
+
G          Float                  Identical to the g format, except that E, rather than e, introduces the exponent
+
                                    (where appropriate).
+
s          String String.
+
</pre>
+
 
+
        F'''lag Specification'''
+
 
+
<pre>
+
Flag      Meaning                                                            Default
+
-          Left align the result within the given field width.                Right align.
+
+          Prefix the output value with a sign (+ or -) if
+
          the output value is of a signed type.                              Sign appears only for negative signed
+
                                                                              values (-).
+
0          If width is prefixed with 0, zeros are added until the
+
          minimum width is reached. If 0 and - appear, the 0 is
+
          ignored. If 0 is specified with an integer format
+
          (i, u, x, X, o, d) the 0 is ignored.                              No padding.
+
Blank      Prefix the output value with a blank if the output value
+
          is signed and positive; the blank is ignored if both the
+
          blank and + flags appear.                                          No blank appears.
+
#          When used with the o, x, or X format, the # flag prefixes
+
          any nonzero output value with 0, 0x, or 0X, respectively.          No blank appears.
+
#          When used with the e, E, or f format, the # flag forces
+
          the output value to contain a decimal point in all cases.          Decimal point appears only if digits follow it.
+
#          When used with the g or G format, the # flag forces the
+
          output value to contain a decimal point in all cases
+
          and prevents the truncation of trailing zeros.
+
          Ignored when used with c, d, i, u, or s.                          Decimal point appears only if digits follow it.
+
                                                                              Trailing zeros are truncated.  
+
</pre>
+
  
 +
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">
$result = Hex(1033);
+
$val = fmt("Value is '0x\{0:x}'", (int)100);
println($result); // prints "409"
+
println($val); // Prints 0x64 since the {0:x} is telling it to place param 0 as :x meaning hex
+
$result = Hex(1033, 4);
+
println($result); // prints "0409"
+
+
$result = Hex(1033, 8);
+
println($result); // prints "00000409"
+
</syntaxhighlight>
+
 
+
Example with cast on a $variable
+
 
+
<syntaxhighlight lang="sputnik">
+
$var = 1033;
+
$result = Hex((int)$var);
+
println($result); // prints "409"
+
 
+
$result = Hex((int)$var, 4);
+
println($result); // prints "0409"
+
 
+
$result = Hex((int)$var, 8);
+
println($result); // prints "00000409"
+
 
</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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox