:@sprintf

@sprintf("%Fmt", args...)

Return @printf formatted output as string.

julia> s = @sprintf "this is a %s %15.1f" "test" 34.567;

julia> println(s)
this is a test            34.6

Examples

In the Julia programming language, the @sprintf macro is used for formatted string interpolation.

  1. Basic usage:

    julia> @sprintf("The value of pi is %.2f", π)
    "The value of pi is 3.14"

    This example uses @sprintf to interpolate the value of π into a formatted string. The format specifier %.2f is used to display the value of π with two decimal places.

  2. Interpolate multiple values:

    julia> x = 10;
    julia> y = 20;
    julia> @sprintf("The sum of %d and %d is %d", x, y, x + y)
    "The sum of 10 and 20 is 30"

    Here, @sprintf is used to interpolate multiple values into a formatted string. The format specifiers %d are used for integer values.

  3. Use format specifiers for different types:
    julia> name = "Alice";
    julia> age = 30;
    julia> @sprintf("%s is %d years old.", name, age)
    "Alice is 30 years old."

    This example demonstrates the use of %s for string interpolation and %d for integer interpolation.

Common mistake example:

julia> @sprintf("The value of pi is %.2f", "π")
ERROR: MethodError: no method matching sprintf(::String, ::String)

In this example, the value being interpolated ("π") is a string instead of a numeric value. @sprintf expects numeric values to be interpolated with format specifiers like %f or %d. Make sure to provide the appropriate type of value for interpolation to avoid such errors.

See Also

:@printf, :@sprintf, display, displayable, dump, info, isprint, print, println, print_escaped, print_joined, print_shortest, print_unescaped, print_with_color, pushdisplay, redisplay, show, showall, showcompact, sprint, versioninfo,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: