I’m fairly sure this topic has come up before, either on discourse, or on the old google group, but I can’t seem to find the link. Regardless, I thought I might see if the answer has changed given all the language developments over the past few years.
Given the following code:
x = rand() @sprintf("%2.2f", x) fmt_str = "%2.2f" @sprintf(fmt_str, x)
the first call to
@sprintf produces an appropriately formatted string. The second call generates an argument error. My understanding is that this is because the format string input to the
@sprintf macro can not be generated at run-time (ie it needs to be known at compile time). Is this correct?
Given v0.7, what options are available to me If I really wanted to convert
String using a run-time generated format string? What if I do not care about speed (ie the section of code that will do this stuff is not performance-critical)?
I am honestly too embarrassed to post the code that shows how I currently deal with this issue. Let’s just say it involves a lot of
if statements and takes an impressive amount of time to compile…
Also, I am willing to entertain the suggestion that I need to re-think my workflow such that I no longer care about converting floating point numbers to strings using run-time generated format strings… but I haven’t worked out a way to do this yet. Currently I like doing this because I have fairly general routines that, deep within, print interesting output for me to read, and sometimes I like being able to control what that output will look like with some options passed in at the beginning of the routine.
Cheers, and thanks in advance to all responders.