That @sprintf is a macro sort of explains why using a run-time value
doesn't work in the same way, but it isn't really *the* reason since
@sprintf(fmt,
val) could work in principle – it would just have to delegate to a function
if its argument isn't a compile-time string.
If using a run-time string is particularly useful to you, I'd suggest
opening an issue about this, since it appears to be missing functionality.
On Saturday, 12 April 2014 07:56:54 UTC+1, John Myles White wrote:
>
> @sprintf is a macro, not a function. It doesn't evaluate its inputs: it
> just rewrites the inputs into something else (usually less readable) that
> carries out the actual computation. You can see what it does using the
> macroexpand function:
>
> julia> macroexpand(quote @sprintf("%8.1e", 3.1415) end)
> :(begin # none, line 1:
> Base.Printf.sprint(#63#io->begin # printf.jl, line 783:
> begin
> #59#out = #63#io
> #60###x#3463 = 3.1415
> local #61#neg, #57#pt, #58#len, #62#exp
> if Base.Printf.isfinite(#60###x#3463)
> Base.Printf.ini_dec(#60###x#3463,2)
> #61#neg = Base.Printf.NEG[1]
> #62#exp =
> Base.Printf.-(Base.Printf.POINT[1],1)
>
> (Base.Printf.-(Base.Printf.-(1,Base.Printf.|((#62#exp Base.Printf.<=
> -100),(100 Base.Printf.<= #62#exp))),#61#neg) Base.Printf.> 0) &&
> Base.Printf.write(#59#out,' ')
> #61#neg && Base.Printf.write(#59#out,'-')
>
> Base.Printf.write(#59#out,Base.Printf.DIGITS[1])
> Base.Printf.write(#59#out,'.')
>
> Base.Printf.write(#59#out,Base.Printf.+(Base.Printf.pointer(Base.Printf.DIGITS),1),1)
>
>
> Base.Printf.write(#59#out,'e')
> Base.Printf.print_exp(#59#out,#62#exp)
> else
> Base.Printf.write(#59#out,begin # printf.jl,
> line 141:
> if Base.Printf.isnan(#60###x#3463)
> " NaN"
> else
> if (#60###x#3463 Base.Printf.< 0)
> " -Inf"
> else
> " Inf"
> end
> end
> end)
> end
> Base.Printf.nothing
> end
> end)
> end)
>
> -- John
>
> On Apr 11, 2014, at 11:46 PM, Dominique Orban
> <[email protected]<javascript:>>
> wrote:
>
> > As a follow-up question, why is the following not allowed?
> >
> > julia> fmt = "%8.1e";
> >
> > julia> @sprintf(fmt, 3.1415)
> > ERROR: first or second argument must be a format string
> >
> > I don't see how it's different from
> >
> > julia> @sprintf("%8.1e", 3.1415)
> >
> > What's the appropriate syntax?
> >
> > Thanks.
> >
> >
> > On Friday, April 11, 2014 11:24:50 PM UTC-7, Dominique Orban wrote:
> > Thank you! Such a basic operation could feature a bit more prominently
> in the documentation.
> >
> >
> > On Friday, April 11, 2014 11:21:28 PM UTC-7, John Myles White wrote:
> > @sprintf
> >
> > On Apr 11, 2014, at 11:18 PM, Dominique Orban <[email protected]>
> wrote:
> >
> >> Sorry if this is a RTFM, but I can't find the answer in the
> documentation or on the web. I may have missed it. I come from Python where
> I can build strings with formatted data using a syntax like
> >>
> >> s = "pi=%7.1e" % acos(-1)
> >>
> >> How do I accomplish that in Julia? @printf doesn't do the job because
> it doesn't return anything:
> >>
> >> julia> s = @printf("%7.1e", 3.14)
> >> 3.1e+00
> >> julia> s
> >>
> >>
> >>
> >>
> >> Thanks.
> >>
> >
>
>