Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Base.allignment method #739

Open
rafaqz opened this issue Sep 7, 2024 · 0 comments · May be fixed by #752
Open

Add a Base.allignment method #739

rafaqz opened this issue Sep 7, 2024 · 0 comments · May be fixed by #752

Comments

@rafaqz
Copy link
Contributor

rafaqz commented Sep 7, 2024

Currently unit printing doesn't line up the decimal point like most other Numbers do:

julia> [1.99809802u"s" 2.339098; 1.0 2.0u"km/hr"]
2×2 Matrix{Quantity{Float64}}:
 1.9981 s        2.3391
      1.0  2.0 km hr^-1

Thats because Unitful is missing a Base.alignment method:

help?> Base.alignment
  alignment(io, X) returns a tuple (left,right) showing how many characters are needed on either side of an alignment feature such as a decimal point.

Like this:

function Base.alignment(io::IO, x::Unitful.Quantity)
    # This gives us how long a Number is in show - its all on the left
    length = Base.alignment_from_show(io, x)[1]
    # Then get where the alignment of the parent Real:
    left, right = Base.alignment(io, ustrip(x))
    # Then return the left from the parent real, and the right that combines
    # the parents decimal places, and the printing of the units.
    return left, right + length - (left + right)
end

Which fixes printing:

julia> [1.99809802u"s" 2.339098; 1.0 2.0u"km/hr"]
2×2 Matrix{Quantity{Float64}}:
 1.9981 s  2.3391
 1.0       2.0 km hr^-1

This also fixes DimensionalData.jl printing of Unitful.jl lookups, which currently look pretty bad.

See: ggebbie/OceanGreensFunctionMethods.jl#22

I don't currently have time to write up a PR and follow it through, so leaving this here if anyone else wants to, before I forget about it.

@sostock sostock linked a pull request Nov 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant