Skip to content

Commit abbcdec

Browse files
committed
Improve @register_unit docstring and add to docs
1 parent 9b1cba4 commit abbcdec

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

docs/src/units.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ Units.T
4242
Units.L
4343
Units.bar
4444
```
45+
46+
## Custom Units
47+
48+
You can define custom units with the `@register_unit` macro:
49+
50+
```@docs
51+
@register_unit
52+
```

src/register_units.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ end
2020
2121
Register a new unit under the given symbol to have
2222
a particular value.
23+
24+
# Example
25+
26+
```julia
27+
julia> @register_unit MyVolt 1.5u"V"
28+
```
29+
30+
This will register a new unit `MyVolt` with a value of `1.5u"V"`.
31+
You can then use this unit in your calculations:
32+
33+
```julia
34+
julia> x = 20us"MyVolt^2"
35+
20.0 MyVolt²
36+
37+
julia> y = 2.5us"A"
38+
2.5 A
39+
40+
julia> x * y^2 |> uconvert(us"W^2")
41+
281.25 W²
42+
43+
julia> x * y^2 |> uconvert(us"W^2") |> sqrt |> uexpand
44+
16.77050983124842 m² kg s⁻³
45+
```
46+
2347
"""
2448
macro register_unit(symbol, value)
2549
return esc(_register_unit(symbol, value))

0 commit comments

Comments
 (0)