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?
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.
The sprintf() is an in-built function of PHP which writes a formatted string to a variable. It returns a formatted string.
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.
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 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.
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.
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.
@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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With