Skip to content

Commit

Permalink
add docs for normalize_units and convert_unit
Browse files Browse the repository at this point in the history
  • Loading branch information
longemen3000 committed Sep 24, 2020
1 parent 9b5bd32 commit 27c652f
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
"""
normalize_units(x)
normalize_units(val)
On normal numbers, it is the identity, but on numbers or vectors of `Unitful.Quantity`,it converts the unit to an equivalent SI unit and strips the unit information.
For normal numbers, this is the identity function.
For `Unitful` quantities, it converts to SI units and strips the `Unitful` type.
# Examples
```julia-repl
julia> normalize_units(0.0u"°C")
273.15
```
```julia-repl
julia> normalize_units(273.15)
273.15
```
"""
normalize_units(x) = Unitful.ustrip(Unitful.upreferred(x))
normalize_units(x::AbstractVector) = Unitful.ustrip.(Unitful.upreferred.(x))
Expand All @@ -26,6 +34,27 @@ function mw_div(x,mw)
return 1000.0*x/mw
end

"""
convert_unit(from,to,val)
Converts an unit from the unit stored in `from` to the unit stored in `to`.
When both units are equal, it justs returns `val`.
If `val` itself is an `unit`, then it converts the from the unit in `val` to the unit in `to`.
# Examples
```julia-repl
julia> convert_unit(u"Pa",u"kPa",1000.0)
1.0
```
```julia-repl
julia> convert_unit(u"Pa",u"kPa",1u"atm")
4053//40
```
"""
function convert_unit end
function convert_unit(from::T,to::T,val::N) where {T,N<:Number}
return val
end
Expand Down

2 comments on commit 27c652f

@longemen3000
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/21950

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 27c652f789f850219bc3835d2a96cdc917c12ea0
git push origin v0.3.0

Please sign in to comment.