# Unitful arguments to functions?

**URL:** https://discourse.julialang.org/t/unitful-arguments-to-functions/34382
**Category:** General Usage
**Tags:** functions, unitful
**Created:** [February 9, 2020, 7:32pm UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382 "2020-02-09T19:32:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![angelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angelv/32/133_2.png) [@angelv](https://discourse.julialang.org/u/angelv)
#### Post date: [February 9, 2020, 7:32pm UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/1 "2020-02-09T19:32:54Z")

</div>

Hi,

just started using Unitful.jl, and it looks great. I wanted to define a function that takes, as a first argument, some length, and as second argument something inverse length. First one is no problem, but despite trying all variations that came to my mind and looking for info in the Unitful documentation, I didn’t find how to do it. So, ideally I would like to do something like:

> f1(a::Unitful.Length, b::Unitful.Length^-1) = […]

but I don’t seem to get the syntax right for the second argument.

Any help appreciated,

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [February 9, 2020, 10:43pm UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/2 "2020-02-09T22:43:32Z")

</div>

i had this laying around

```julia
function f1(a::Unitful.Quantity{<:Real, Unitful.𝐋^1}, b::Unitful.Quantity{<:Real, Unitful.𝐋^(-1)})
return 0 
end

```

if you want more ease of writing, you can use a type const:

```julia
const INV_LENGTH = Unitful.Quantity{<:Real, Unitful.𝐋^(-1)}
function f1(a::Unitful.Length, b:INV_LENGTH)
return 0 
end

```

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [February 10, 2020, 3:11am UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/3 "2020-02-10T03:11:03Z")

</div>

The relevant part of the manual is at  
[https://painterqubits.github.io/Unitful.jl/stable/newunits/#Unitful.@derived\_dimension](https://painterqubits.github.io/Unitful.jl/stable/newunits/#Unitful.@derived_dimension)

You use the `@derived_dimension` macro.

`@derived_dimension InvLength 𝐋^-1`  
or  
`@derived_dimension InvLength dimension(u"1/m")`

Then your function would be `f1(a::Unitful.Length, b::InvLength) = ...`

---

<div class="post-metadata">

### Author: ![angelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angelv/32/133_2.png) [@angelv](https://discourse.julialang.org/u/angelv)
#### Post date: [February 10, 2020, 10:34am UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/4 "2020-02-10T10:34:59Z")

</div>

Many thanks to both.

`@derived_dimension InvLength dimension(u"1/m")` works very nice and easy on the eyes.

Cheers,

---

<div class="post-metadata">

### Author: ![angelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angelv/32/133_2.png) [@angelv](https://discourse.julialang.org/u/angelv)
#### Post date: [February 11, 2020, 11:21am UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/5 "2020-02-11T11:21:16Z")

</div>

And is it possible to round the result to a given number of digits? The following doesn’t work

```julia

round(656.1122u"nm",digits=2)

```

and something like this works but probably there is a better way…

```julia

round_uful(uful,digs) = round(ustrip(uful),digits=digs) * unit(uful)

```

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [February 11, 2020, 1:01pm UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/6 "2020-02-11T13:01:19Z")

</div>

> [@angelv](#):
>
> And is it possible to round the result to a given number of digits? The following doesn’t work
> 
> ```julia
> round(656.1122u"nm",digits=2)
> 
> ```

```julia
julia> round(u"nm", 656.1122u"nm", digits=2)
656.11 nm

```

---

<div class="post-metadata">

### Author: ![angelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angelv/32/133_2.png) [@angelv](https://discourse.julialang.org/u/angelv)
#### Post date: [February 11, 2020, 2:03pm UTC](https://discourse.julialang.org/t/unitful-arguments-to-functions/34382/7 "2020-02-11T14:03:53Z")

</div>

Many thanks
