# Make Unitful simplify expressions like 1μm/1m to a unitless value by default

**URL:** <https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866>\
**Category:** General Usage\
**Tags:** unitful\
**Created:** [November 21, 2021, 6:47pm UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866 "2021-11-21T18:47:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)\
**Post date:** [November 21, 2021, 6:47pm UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/1 "2021-11-21T18:47:31Z")

</div>

I freguently perform computations like 1μm/1m and would like Unitful to return a unitless number by default rather than having to do NoUnits(1μm/1m).

Is there a setting to make this the default?

---

<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:** [November 22, 2021, 2:07am UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/2 "2021-11-22T02:07:29Z")

</div>

I’m not sure you can set a default, but FWIW, you might not need to, e.g.:

```julia
julia> using Unitful: μm, m

julia> 1μm/1m + 0
1.0e-6

```

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [November 22, 2021, 5:51am UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/3 "2021-11-22T05:51:13Z")

</div>

For this purpose, I’m using a small piece of code listed below. It allows me to use for example `//` as a unit stripping operator.

After including the code below, I can just define the unit stripping operator and use it in a pretty readable way - this way I keep variables tagged with units as long as possible and strip the units just before printing or plotting, so I know exactly if I’m plotting in ns, microseconds or seconds

```julia
julia> @stripunits_operator //
julia> u = 15u"V"; d = 5u"mm"; E = u/d
julia> println("The field intensity is \$(E // u"V/m") [V/m] in a gap of \$(d // u"m") [m]")
The field intensity is 3000.0 [V/m] in a gap of 0.005 [m]

julia> typeof(E // u"V/m")
Float64

```

The code to include:

```julia
function stripunits(a::Unitful.Quantity, u::Unitful.Units)
    try
        return _stripunits(a,u)
    catch
       throw(ArgumentError("Cannot convert $a / $u to a Float64"))
    end
end

function _stripunits(a::Unitful.Quantity{T,D,UA}, u::Unitful.Units{UU,D}) where {T, D, UA, UU}
    return a.val * Float64(Unitful.convfact(u, UA()))
end

macro stripunits_operator(op)
    blk = quote
        import Base.$op
        function Base.$op(a::Unitful.Quantity{T,D,UA}, u::Unitful.Units{UU,D}) where {T, D, UA, UU}
            _stripunits(a, u)
        end
    end
    push!(blk.args, :nothing)
    blk.head = :toplevel
    return blk
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 22, 2021, 7:05am UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/4 "2021-11-22T07:05:24Z")

</div>

Would a “unit simplification” function be useful? For example, something like:

```julia
usimplify(x) = typeof(dimension(x)) == Unitful.Dimensions{()} ? ustrip(x) : x

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 22, 2021, 7:35am UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/5 "2021-11-22T07:35:55Z")

</div>

I like

```julia
x |> upreferred

```

If it’s unitless, that will strip units. If it’s united it will make it SI. Very useful.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 22, 2021, 7:56am UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/6 "2021-11-22T07:56:43Z")

</div>

Perhaps, one thing that might be annoying about `upreferred` is the zeal it can have with accuracy:

```julia
3u"μm" |> upreferred

3//1000000 m

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 22, 2021, 11:21am UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/7 "2021-11-22T11:21:08Z")

</div>

You could overload `show` for Unitful `Quantity` to perform the simplification before printing the value to the terminal or otherwise displaying it.

---

<div class="post-metadata">

**Author:** ![runjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/runjaj/32/23628_2.png) [@runjaj](https://discourse.julialang.org/u/runjaj)\
**Post date:** [November 22, 2021, 4:06pm UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/8 "2021-11-22T16:06:17Z")

</div>

You can solve this easily:

```julia
float(3u"μm" |> upreferred)

3.0e-6 m

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 22, 2021, 4:37pm UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/9 "2021-11-22T16:37:48Z")

</div>

`3u"μm" |> upreferred |> float` if you don’t want to mix styles.

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [November 30, 2021, 4:02pm UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/10 "2021-11-30T16:02:56Z")

</div>

> **[Conversion/promotion · Unitful.jl](https://painterqubits.github.io/Unitful.jl/stable/conversion/)**
>
> Documentation for Unitful.jl.

> Exact conversions between units are respected where possible. If rational arithmetic would result in an overflow, then floating-point conversion should proceed. **Use of floating-point numbers inhibits exact conversion.**

```julia
julia> 3.0u"μm" |> upreferred
3.0e-6 m

```

---

<div class="post-metadata">

**Author:** ![Deduction42](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deduction42/32/9206_2.png) [@Deduction42](https://discourse.julialang.org/u/Deduction42)\
**Post date:** [May 9, 2026, 1:10pm UTC](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/11 "2026-05-09T13:10:23Z")

</div>

I saw this thread after I posed my new version announcement for [FlexUnits.jl](https://discourse.julialang.org/t/ann-flexunits-jl-0-5-unit-simplification-and-log-units/137045) which includes this feature. It simplifies units better than `upreferred` which can give you rather opaque results with a lot of electrical units. You can even use `display_simplified_units(true)` to set it to simplify units by default when displaying.

FlexUnits.jl follows much of Unitful’s convention so it shouldn’t be too difficult for most people to pick up. It matches Unitful for speed in statically inferrable cases, but absolutely dominates Unitful for cases where units can’t be inferred (this bothered me a lot because I often build configurable systems where I have to parse units as string, which completely wrecks performance, FlexUnits solves that problem with Unitful as well as many others).
