FANDOM


Returns a formatted string that has been transformed according to the rules of the C "printf()" function.

Syntax:

format(<baseString> [, <valuesList>])

Examples:

put format("Hello world")
put format("Hello\nworld")
put format("%1.3e",865.3)
put format("Hello world")
put format("%45d",5)

Use the format function to create strings in special formats.

The format function works by taking a baseString that contains formatting incantations, transforming each member of the valuesList according to the corresponding incantation, then substituting the transformed string for the incantation in the baseString. It also transforms C escape sequences in the baseString into their single-character equivalents.

The valid incantations are as follows:

  • String:%[charLength]s The corresponding value is unchanged, except that if a charLength is specified, if the string is shorter than the charLength, enough leading spaces are added to make it charLength characters long. If the length of the string is equal to or greater than the charLength, it is unchanged. For example, the incantation %3s transforms "H" to " H".
  • Character:%[charLength]c The corresponding value is treated as an ASCII value and translated to the corresponding character. If a charLength is specified, one fewer leading spaces are added to make it charLength characters long. For example, the incantation %2c transforms 65 to " A" (65 is the ASCII value of the character "A" ).
  • Decimal number: %[charLength]d
  • Unsigned integer: %[charLength]u Like %[charLength]d, except that if the value is negative, it is subtracted from the largest long integer allowed by the current operating system. (On most operating systems, this is 2^32, or 4,294,967,296.)
  • Octal:%[charLength]o
  • Hexadecimal:
    • %[charLength]x
    •  %[charLength]X Like %[charLength]x, except that the hex digits A-F are given in uppercase. For example, the incantation %4x transforms "30. 3" to " 1E".
  • Floating-point:
    •  %[charLength].[precision]f
    •  %[charLength. precision]g Like %[charLength]. [precision]f, except that trailing zeroes are not added if the number of digits is less than the precision.
  • Scientific notation:
    •  %[charLength. precision]e
    •  %[charLength. precision]E Like %[charLength. precision]e, except that the "E" separating the number from the power of ten is uppercase. For example, the incantation %3. 2e transforms "232. 4" to "2. 32E+02".

If any of the following C escape sequences are present in the baseString, the format function transforms them to the equivalent character:

  • \a = Control-G (bell)
  • \b = Control-H (backspace)
  • \f = Control-L (formfeed)
  • \n = Control-J (linefeed)
  • \r = Control-M (return)
  • \t = Control-I (tab)
  • \v = Control-K (vertical tab)
  • \\ = \ (backslash)
  • \? = ?
  • \' = '
  • \" = " (enclose a quote within a string)
  • \nnn = character whose ASCII value is the octal number represented by nnn
  • \xnn = character whose ASCII value is the hex number represented by xnn

Transformation of values in valuesList uses the standard LiveCode conversion rules. For example, if empty is passed for a numeric incantation it will be taken as the value 0.

Parameters:

  • baseString (string): A string that can include one or more special formatting incantations.
  • valuesList: A list of strings (or expressions that evaluate to strings), separatedby commas. The valuesList contains as many values as there areincantations in the baseString.
  • Returns: The format function returns a string.

See also: charToNum (function),baseConvert (function),numToChar (function),HTMLText (property),numberFormat (property),

Community content is available under CC-BY-SA unless otherwise noted.