# Unit conversion

**URL:** <https://discourse.julialang.org/t/unit-conversion/35302>\
**Category:** General Usage\
**Tags:** package\
**Created:** [February 28, 2020, 9:19pm UTC](https://discourse.julialang.org/t/unit-conversion/35302 "2020-02-28T21:19:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [February 28, 2020, 9:19pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/1 "2020-02-28T21:19:49Z")

</div>

Hi,

I am new to Julia and am trying out Unitful. I would like to do the following:

> a = 1"m" / 1"cm"

and have it return 100.

What must I do to make this happen? It is clearly not automatic. Similarly, I would like:

> b = 1"in"

to return 0.003937 \* u"m"

In other words, I would like an automatic convert to SI units.

Thanks,

Gordon

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 28, 2020, 9:30pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/3 "2020-02-28T21:30:33Z")

</div>

Try `upreferred()` IIRC

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [February 28, 2020, 9:32pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/4 "2020-02-28T21:32:40Z")

</div>

I think automatic conversion is not recommended and so the Unitful devs opted for not doing it. For SI units conversion, I think `upreferred` generally does the trick:

```julia
julia> a = 1u"m" / 1u"cm"
1.0 m cm^-1

julia> upreferred(a)
100.0

julia> b = 1.0u"inch"
1.0 inch

julia> upreferred(b)
0.0254 m

```

But I think being explicit with your conversion is probably a better idea, and the pipe operator offers nice syntax for this IMHO:

```julia
julia> a |> unit(1) # maybe there is a better unitless unit
100.0

julia> b |> u"m"
0.0254 m

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 28, 2020, 9:35pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/5 "2020-02-28T21:35:41Z")

</div>

```julia
julia> uconvert(NoUnits,1u"m"/1u"cm")
100.0
julia> uconvert(u"m",1.0u"inch")
0.0254 m

```

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [February 28, 2020, 9:39pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/6 "2020-02-28T21:39:56Z")

</div>

Thank you very much for your replies. Yes, I concluded the same, but was wondering whether I had missed some capability. I appreciate the quick response!

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [February 28, 2020, 10:16pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/7 "2020-02-28T22:16:09Z")

</div>

This also works:

```julia
julia> 1u"m"/1u"cm" |> u"m/m"
100.0

```

which feels like an assertion that it’s a ratio of lengths, but it’s actually only asserting that it is unitless. Note that this works as well:

```julia
julia> 1u"m"/1u"cm" |> u"V/V"
100.0

```

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [February 28, 2020, 11:32pm UTC](https://discourse.julialang.org/t/unit-conversion/35302/8 "2020-02-28T23:32:54Z")

</div>

Thanks. Is there a method to convert any unit into SI units? So far, all I seen is how to convert from one unit to another, but I have to specify the target units.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 29, 2020, 2:18am UTC](https://discourse.julialang.org/t/unit-conversion/35302/9 "2020-02-29T02:18:42Z")

</div>

… `upreferred`? Is there any case where it doesn’t convert to SI?

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [February 29, 2020, 2:21am UTC](https://discourse.julialang.org/t/unit-conversion/35302/10 "2020-02-29T02:21:53Z")

</div>

I think the problem is that saying “convert to SI” isn’t specific enough. Do you want Newtons? Or kg\*m/s^2? I think that’s why you have to specify.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [February 29, 2020, 2:40am UTC](https://discourse.julialang.org/t/unit-conversion/35302/11 "2020-02-29T02:40:31Z")

</div>

Excellent point. Thanks.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 29, 2020, 4:50am UTC](https://discourse.julialang.org/t/unit-conversion/35302/12 "2020-02-29T04:50:51Z")

</div>

By the way, there is SI units with lengths in meters, and SI units with lengths in millimeters. Both are in common use.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [February 29, 2020, 4:57am UTC](https://discourse.julialang.org/t/unit-conversion/35302/13 "2020-02-29T04:57:54Z")

</div>

Thanks. I really thought meters was some kind of default. I come from the Brian environment in Python, and I just got used to it.

Gordon

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 29, 2020, 6:30am UTC](https://discourse.julialang.org/t/unit-conversion/35302/14 "2020-02-29T06:30:41Z")

</div>

It’s easy to convert to base SI units without ambiguity. The base units are m, s, kg, K, A, mol, cd. The odd one out is kg, the others are without prefix.

Normalizing should be straightforward.
