Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use function argument in `@sprintf` in Julia?

The following errors:

function num_to_string(x,format="%.1f")
    @sprintf format x
end

The error is:

LoadError: MethodError: no method matching Printf.Format(::Symbol)

I tried using the @sprintf(format,x) form, as well as interpolating(?) like @sprintf $format x

How can I use variables in the @sprintf format?

like image 656
Alec Avatar asked Nov 26 '21 04:11

Alec


People also ask

What sprintf () function actually does?

The sprintf() function formats and stores a series of characters and values in the array buffer. Any argument-list is converted and put out according to the corresponding format specification in the format-string.

What is the use of sprintf () function in PHP 1 point?

The sprintf() is an in-built function of PHP which writes a formatted string to a variable. It returns a formatted string.

What does sprintf return?

The sprintf function returns the number of characters stored in the array s , not including the terminating null character. The behavior of this function is undefined if copying takes place between objects that overlap—for example, if s is also given as an argument to be printed under control of the ' %s ' conversion.

Is it possible to use a variable as a @sprintf argument?

This is good for performance, but means that @sprintf is limited to literal strings as formats, not variables. However, you can directly make the function call that @sprintf ultimately makes, and since that would be an ordinary function call (and not a macro invocation), you can use a variable as the format argument:

How do function arguments work in Julia?

Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions. Function arguments themselves act as new variable bindings (new locations that can refer to values), but the values they refer to are identical to the passed values.

What is @printf in Julia?

Julia uses a different approach: @printfis a macro which translates format strings into efficient code specific to that format specification. If you think about it, a printf-style format string is really just a way to express a function that takes a fixed number and type of arguments and prints them in a particular way.

What does the expression f mean in Julia?

Without parentheses, the expression f refers to the function object, and can be passed around like any other value: As with variables, Unicode can also be used for function names: Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions.


Video Answer


1 Answers

@sprintf is a macro, and converts the format string into a processed Format object during macro-expansion itself. This is good for performance, but means that @sprintf is limited to literal strings as formats, not variables.

However, you can directly make the function call that @sprintf ultimately makes, and since that would be an ordinary function call (and not a macro invocation), you can use a variable as the format argument:

julia> function num_to_string(x,fmt="%.1f")
           Printf.format(Printf.Format(fmt), x)
       end
num_to_string (generic function with 2 methods)

julia> num_to_string(45)
"45.0"

julia> num_to_string(pi, "%.5f")
"3.14159"

like image 143
Sundar R Avatar answered Nov 15 '22 04:11

Sundar R