Unitful.jl
Unitful.jl copied to clipboard
how to control the string-formatted precision?
Feature Request: Since Unitful produces strings, I frequently want to control the precision to avoid
a = (1/7)u"mm"
@printf( "%s", a)
> 0.14285714285714285 mm
workaround:
a = (1/7)u"mm"
@printf( "%s", round(typeof(a), a, digits=3))
> 0.143 mm
Rather than trying to mess with the Base.show() functionality, I think an explicit Unitful-to-string function, possibly taking the printf formatting arguments, would be great.
Also, this overload on round() isn't mentioned in the docs, source with kwargs from round
Thanks
Interestingly DataFrames does some rounding:
julia> DataFrame(a=(1/7)u"mm")
1×1 DataFrame
Row │ a
│ Quantity…
─────┼─────────────
1 │ 0.142857 mm
We could either add a new function for this or use IOContext
properties. The latter is already in use for formatting exponents in units:
julia> io = IOContext(stdout, :fancy_exponent=>true);
julia> println(u"m/s^2")
m s^-2
julia> println(io, u"m/s^2")
m s⁻²
Interestingly DataFrames does some rounding:
DataFrames probably sets the IOContext
property :compact => true
, as containers usually do:
julia> println((1/7)u"mm")
0.14285714285714285 mm
julia> println(IOContext(stdout, :compact=>true), (1/7)u"mm")
0.142857 mm