It would be helpful to have the option to be able to round by significant figures, as opposed to decimal places.
While this can be done with @sprintf("%.3g", pi)
I have found it elegant to work with rounded numerical values.
Perhaps round(x,n;significant=false)
such that:
julia> round(pi,3,significant=true)
3.14
julia> round(pi/10,3,significant=true)
0.314
julia> round(pi,3)
3.142
julia> round(pi,3)
0.314
This functionality seems useful, but I'm not sure about the naming. Do other languages have such a feature?
It seems missing from Python and C# and Fortran. R calls the function signif. Someone wrote a Matlab function called roundsd for this, and someone else wrote sd_round.
It seems better to me to use a different named function instead of a keyword argument.
It would make more sense to me to have two orthogonal keyword options. I.e. the signature would look like round(x, trailing=t, significant=s)
which would round x
to no more than t
trailing digits and no more than s
significant digits. The number of trailing digits defaults to zero while the number of significant digits defaults to infinite (or something effectively equivalent).
This is actually also a fairly sensible interface to generic printing function for floating-point values, which could be part of the long-awaited printf rewrite.
We already have a signif
function. We should add a cross reference from the round
docs though, as this has come up again.
@StefanKarpinski Can we close this now, or should we keep it open to track your suggestion above?
Eh, don't really mind either way.
The printing thing is probably a separate issue.
For reference, now Base.round accepts the sigdigits
keyword:
julia> round(pi, digits=3)
3.142
julia> round(pi, sigdigits=3)
3.14
Most helpful comment
For reference, now Base.round accepts the
sigdigits
keyword: