Skip to content

Proposal for rounding with numerical Quantity resolution: round(precision::Quantity, x::quantity) #836

@awbertulli

Description

@awbertulli

Hi, thank you for your work on Unitful.jl. I was looking for a way to round a Quantity up to a certain resolution, and i discovered there isn't method defined. We can round up to a certain unit, but not a measure. Meaning, we can round a meter up to a cm, but not up to the nearest 5cm measure. May i suggest this:

function round::Quantity, x::Quantity; kwargs...)
    if ustrip(δ) == 0
        round(unit(δ), x; kwargs...)
    else
        round(x / δ, RoundNearest; kwargs...) * δ
    end
end

julia> round(5u"cm", 1u"m")
100.0 cm

julia> round(5u"cm", 0.94u"m")
95.0 cm

EDIT: this is similar, but different, to #326

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions