以下錯誤:
function num_to_string(x,format="%.1f")
@sprintf format x
end
錯誤是:
LoadError: MethodError: no method matching Printf.Format(::Symbol)
我嘗試使用@sprintf(format,x)表格,以及插值(?)@sprintf $format x
如何使用@sprintf格式中的變數?
uj5u.com熱心網友回復:
@sprintf是一個宏,并Format在宏擴展本身期間將格式字串轉換為處理物件。這對性能有好處,但意味著@sprintf僅限于文本字串作為格式,而不是變數。
但是,您可以直接進行@sprintf 最終make 的函式呼叫,并且由于這將是一個普通的函式呼叫(而不是宏呼叫),您可以使用一個變數作為格式引數:
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"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368342.html
上一篇:我遇到了與C 中的字串相關的問題
下一篇:Stdout:用戶檔案名輸入
