[ CCode ( cname = "g_strdup_printf" , instance_pos = -1 ) ]
public string to_string (string format = "%g")

Similar to the standard C sprintf function but safer, since it calculates the maximum space required and allocates memory to hold the result.

The returned string should be freed with g_free when no longer needed.

The returned string is guaranteed to be non-NULL, unless format contains `lc` or ` ls` conversions, which can fail if no multibyte representation is available for the given character.

Example: Float to string:

public static int main () {
float num = 0.123f;
print ("%s\n", num.to_string ());
return 0;

valac --pkg glib-2.0 float.to_string.vala



a standard printf format string, but notice string precision pitfalls


the parameters to insert into the format string


a newly-allocated string holding the result