Skip to content

Commit

Permalink
Fix display() of angular degrees to be SI compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Jul 19, 2019
1 parent ef517f8 commit 2d44add
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ function show(io::IO, x::Unit{N,D}) where {N,D}
show(io, FreeUnits{(x,), D, nothing}())
end

# Space between numerical value and unit should always be included
# except for angular degress, minutes and seconds (° ′ ″)
# See SI 9th edition, section 5.4.3; "Formatting the value of a quantity"
# https://www.bipm.org/utils/common/pdf/si-brochure/SI-Brochure-9.pdf
has_unit_spacing(u) = true
has_unit_spacing(u::Units{(Unit{:Degree, NoDims}(0, 1//1),), NoDims}) = false

"""
show(io::IO, x::Quantity)
Show a unitful quantity by calling `show` on the numeric value, appending a
Expand All @@ -56,7 +63,7 @@ space, and then calling `show` on a units object `U()`.
function show(io::IO, x::Quantity)
show(io,x.val)
if !isunitless(unit(x))
print(io," ")
has_unit_spacing(unit(x)) && print(io," ")
show(io, unit(x))
end
nothing
Expand All @@ -65,18 +72,11 @@ end
function show(io::IO, mime::MIME"text/plain", x::Quantity)
show(io, mime, x.val)
if !isunitless(unit(x))
print(io," ")
has_unit_spacing(unit(x)) && print(io," ")
show(io, mime, unit(x))
end
end

function show(io::IO, x::Quantity{S, NoDims, <:Units{
(Unitful.Unit{:Degree, NoDims}(0, 1//1),), NoDims}}) where S
show(io, x.val)
show(io, unit(x))
nothing
end

function show(io::IO, x::Type{T}) where T<:Quantity
invoke(show, Tuple{IO, typeof(x)}, IOContext(io, :showoperators=>true), x)
end
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,9 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0")
@test repr("text/plain", 1.0 * u"m * s * kg^-1") == "1.0 m s kg^-1"
@test repr(Foo() * u"m * s * kg^-1") == "1 m s kg^-1"
@test repr("text/plain", Foo() * u"m * s * kg^-1") == "42.0 m s kg^-1"
# Angular degree printing #253
@test sprint(show, 1.0°) == "1.0°"
@test repr("text/plain", 1.0°) == "1.0°"
end

@testset "DimensionError message" begin
Expand Down

0 comments on commit 2d44add

Please sign in to comment.