# Is there a generic way to remove the unit from a number?

**URL:** <https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277>\
**Category:** General Usage\
**Tags:** question, unitful\
**Created:** [January 12, 2020, 8:32pm UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277 "2020-01-12T20:32:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [January 12, 2020, 8:32pm UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/1 "2020-01-12T20:32:14Z")

</div>

Is there a generic way to remove the unit from a number?

Note that `x / oneunit(x)` sometimes doesn’t work. For instance, if you look at the `Furlongs` testing example defined in [https://github.com/JuliaLang/julia/blob/master/test/testhelpers/Furlongs.jl](https://github.com/JuliaLang/julia/blob/master/test/testhelpers/Furlongs.jl), you’ll see that `x / oneunit(x)` gives a result of type `Furlog{0}` instead of a unitless number.

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [January 12, 2020, 9:08pm UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/2 "2020-01-12T21:08:27Z")

</div>

Maybe try Unitful.jl package, it has `ustrip` method for that and handles division of units well. For this particular case to unwrap you need to type x.val where x is a Furlong.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 13, 2020, 8:16am UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/3 "2020-01-13T08:16:30Z")

</div>

Generally `Real(x)` should work, but I don’t think it is formally part of an interface spec for unitful quantities (because there isn’t one 😉).

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [January 13, 2020, 10:00am UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/4 "2020-01-13T10:00:33Z")

</div>

A generic interface for unitful quantities that packages abide to would do some good.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 13, 2020, 10:06am UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/5 "2020-01-13T10:06:15Z")

</div>

I agree, but since interface expectations are not written up for `Number` in general this may not be something that can be fixed quickly.

I kind of expected `(one(x) - zero(x)) / oneunit(x)` to work for `Furlong`, but apparently it doesn’t, not sure if that should be reported as an issue.

In general I am not even sure that “removing unit from a number” makes sense for all types which define `oneunit` but not `zero`. I am thinking about dates of course, which count from an epoch which should be considered an implementation detail,

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 13, 2020, 10:37am UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/6 "2020-01-13T10:37:59Z")

</div>

I think `Real(x / oneunit(x))` seems like the right generic approach. You could submit a PR to change it if that doesn’t work.

If `x` has units that are not equivalent to “unitless” (e.g., `Furlong{n}` for `n != 0`), IMO `Real(x)` should throw an error. (`convert(Real, x)` should _definitely_ throw an error, `Real(x)` is a bit less clear.) Are there packages where this (incorrectly) works? If so let’s fix it 😈.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [January 13, 2020, 10:58am UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/7 "2020-01-13T10:58:29Z")

</div>

> [@tim.holy](#):
>
> You could submit a PR to change it if that doesn’t work.

In the `Furlong` example, `x / oneunit(x)` returns a `Furlong{0}`. So in your opinion we should have a `Real(::Furlong{0})` method?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 13, 2020, 11:02am UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/8 "2020-01-13T11:02:30Z")

</div>

Exactly. Or more comprehensively,

```julia
(::Type{R})(x::Furlong{0}) where R<:Real = R(x.val)

```

(untested)

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 19, 2022, 10:31pm UTC](https://discourse.julialang.org/t/is-there-a-generic-way-to-remove-the-unit-from-a-number/33277/9 "2022-12-19T22:31:46Z")

</div>

> [@tim.holy](#):
>
> Real(x / oneunit(x))

if `rho/ρ` is a vector with a unit, the following works for me as well:

```julia
convert.(Float64, ρ ./ oneunit(ρ[1]))

```
