Unitful.jl icon indicating copy to clipboard operation
Unitful.jl copied to clipboard

how to control the string-formatted precision?

Open bc0n opened this issue 3 years ago • 3 comments

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

bc0n avatar Aug 05 '21 03:08 bc0n

Interestingly DataFrames does some rounding:

julia> DataFrame(a=(1/7)u"mm")
1×1 DataFrame
 Row │ a           
     │ Quantity…   
─────┼─────────────
   1 │ 0.142857 mm

tp2750 avatar Feb 14 '22 20:02 tp2750

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⁻²

sostock avatar Oct 30 '22 18:10 sostock

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

sostock avatar Oct 30 '22 18:10 sostock