strformat

From RPTools Wiki
Revision as of 09:50, 14 December 2008 by Craig (talk | contribs) (New page: ==Function strformat== Returns a string formatted based the escape sequences in the passed in string and optional arguments. The format string can contain special instructions that are in...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Function strformat

Returns a string formatted based the escape sequences in the passed in string and optional arguments.

The format string can contain special instructions that are introduced with the % symbol. The easiest and probably most useful format instruction is %{varname} which inserts the value of varname into the string.

The following formats are also supported (lower case format arguments perform the same conversion as the lowercase letters but return the result in uppercase).

  • %h, %H, %x, %X
Inserts the hexadecimal representation of the corresponding argument which must be an integer.
  • %s, %S
Inserts the string representation of the corresponding argument.
  • %d
Inserts the decimal integer representation of the corresponding argument that must be an integer value.
  • %e, %E
Inserts the floating point value from the corresponding argument in computerized scientific notion.
  • %f
Inserts the floating point value from the corresponding argument.
  • %g, %G
Inserts the floating point value from the corresponding argument in computerized scientific notion or decimal format.
  • %a, %A
Inserts the floating point value from the corresponding argument as a hexadecimal floating-point number with a significand and an exponent
  • %%
Inserts a percent symbol.
  • %n
Inserts a new line.

Flags for format conversions

  • - left justified
  • + will always contain a sign character
  • (space) will include leading space for positive values
  • 0 will be zero padded
  • ( will enclose negative numbers in parentheses

Usage

[h: str = strformat(str)]
[h: str = strformat(str, arg, ...)]


Examples

    [h: weaponName = "Long Sword"]
    [h: maxDam = 8]
    [r: strformat("Weapon Name=%{weaponName}; Max Damage=%{maxDam}")]

Returns

   Weapon Name=Long Sword; Max Damage=8


    [strformat("%d", 10)] [strformat("%05d", 10)] [strformat("%+d", 10)]

Returns

   10 00010 +10
    [h: weaponName = "Long Sword"]
    [h: maxDam = 8]
    [r: strformat("Weapon Name=%{weaponName}; Max Damage=%{maxDam}")]

Returns

   Weapon Name=Long Sword; Max Damage=8


    [strformat("%f", -10.502)] [strformat("%g", -10.502)]
    [strformat("%+e", -10.502)] [strformat("%5.1f", -10.502)]
    [strformat("%(5.1f", -10.502)]

Returns

   -10.502000 
   -10.5020 
    -1.050200e+01 
   -10.5 
   (10.5)