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

A tip for specifying precision of displayed quantities.

Open mateuszbaran opened this issue 4 years ago • 3 comments

I've spent about half an hour figuring it out so I think this tip may be useful for other people :slightly_smiling_face: .

mateuszbaran avatar Jul 07 '21 09:07 mateuszbaran

I think the wording here is misleading: round does not “set precision of displayed value”. The round function does not display anything by itself, it returns a rounded value. Describing this as specifying precision for display sounds like it is some equivalent to printf formatting.

I think it would be good to add a section about rounding. In that section, we could explain why it is necessary to specify the output unit (or is it obvious enough?).

Note that you can also specify a unit instead of a type as first argument. We should document this as well:

julia> round(typeof(d), d; sigdigits=3)
1.74 mm

julia> round(u"mm", d; sigdigits=3)
1.74 mm

julia> round(u"m", d; sigdigits=3)
0.00174 m

sostock avatar Jul 07 '21 09:07 sostock

"set precision of displayed value" is the thing I wanted to do, so to me this is the key part that needs documenting. If you thing this should be done differently than using round, I'm open to suggestions.

mateuszbaran avatar Jul 07 '21 09:07 mateuszbaran

I had a quantity that, when printed it to a string, it gave me way too many digits, more than the precision with which the quantity was determined. So it looked silly and I wanted to trim it -- and it's definitely not obvious how it should be done.

mateuszbaran avatar Jul 07 '21 09:07 mateuszbaran