Main Index : Reference :

tostring()


Description

Converts a variable of any type to a string.

Definition

[string] tostring([any type] x)
[string] tostring([any type] x,[string] format)

Parameters

x can be of any type.
format is an optional string that specifies the formatting.

Return Value

tostring() returns the parameter x converted to a string. The different types are by default formatted as below:

Type Example
int 894716
float 16.847000
vector [13.756000,78.137000,44.863000]
string Hello World

All other types are written between brackets, like this: [Array-1e50e14].

Optionally, integers and floats (not vectors) can be formatted using the format string. The formatting syntax is similar to the format field in the C function printf().

Example

// Outputs the table above.
//
// (N.B. Actually tostring() isn't needed since println()
// automatically converts its parameters to strings!)

var a = 894716;
var b = 16.847;
var c = vector(13.756,78.137,44.863);
var d = "Hello World";

println("int     ",tostring(a));
println("float   ",tostring(b));
println("vector  ",tostring(c));
println("string  ",tostring(d));


// A common use of tostring() is in conjunction with stradd(), when
// preparing a message to the user, if all types aren't strings.
//
// Displays "There are 47 items.".

var n = 47;

var message = stradd("There are ",tostring(n)," items.");
TextDialog(message);


// Some examples of using the formatting string.
// See a C printf documentation for more information.

// Four digits hexadecimal. Outputs "20F8".
println(tostring(8440,"04X"));

// Three decimals float. Outputs "47.500".
println(tostring(47.5,".3f"));

// Three decimals with exponent. Outputs "1.846e+007".
println(tostring(18461947.23,".3e"));

See Also

[string]