# Percent written as x%

**URL:** <https://discourse.julialang.org/t/percent-written-as-x/123735>\
**Category:** General Usage\
**Tags:** question, unitful\
**Created:** [December 12, 2024, 1:16am UTC](https://discourse.julialang.org/t/percent-written-as-x/123735 "2024-12-12T01:16:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 12, 2024, 1:16am UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/1 "2024-12-12T01:16:47Z")

</div>

```julia
macro %(x)
    Unitful.percent(.01x) 
end

@% 12 #12 %

```

Is there a function or macro that would allow 12% instead of @% 12

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 12, 2024, 1:55am UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/2 "2024-12-12T01:55:10Z")

</div>

> [@Lincoln\_Hannah](#):
>
> Is there a function or macro that would allow 12% instead of @% 12

No, because `%` is parsed as a binary operator (by default defined as a synonym for `rem`). Functions and macros can’t change how things are parsed, only what they _do_.

You could define

```julia
struct PCT; end
const pct = PCT()
Base.:*(x, ::PCT) = Unitful.percent(x/100)

```

which would allow you to do e.g.

```julia
julia> 12pct
12.0 %

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 12, 2024, 1:54pm UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/3 "2024-12-12T13:54:08Z")

</div>

While I’d probably recommend @stevengj’s approach, you _could_ do the following if you really like the `%` symbol:

```julia
# define our own local shadow of %
(%)(args...) = Base.:(%)(args...)
Base.:(*)(x, ::typeof(%)) = Unitful.percent(x/100)

```

and then you can write

```julia-repl
julia> 12.0(%)
12.0 %

```

and

```julia-repl
julia> let y = 12.0
           y*(%)
       end
12.0 %

```

Just another fun option.

---

<div class="post-metadata">

**Author:** ![DanDeepPhase](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandeepphase/32/24076_2.png) [@DanDeepPhase](https://discourse.julialang.org/u/DanDeepPhase)\
**Post date:** [December 12, 2024, 2:44pm UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/4 "2024-12-12T14:44:14Z")

</div>

I often export the units directly (frequently with `using Unitful.DefaultSymbols`):

```julia
julia> using Unitful: percent

julia> 25percent
25 %

julia> 25percent |> NoUnits
1//4

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 12, 2024, 2:58pm UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/5 "2024-12-12T14:58:12Z")

</div>

You can also abuse unicode if you want this to be write-once:

```julia-repl
julia> using Unitful: percent as ᵒ╱ₒ

julia> 25ᵒ╱ₒ
25 %

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 12, 2024, 10:40pm UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/6 "2024-12-12T22:40:22Z")

</div>

```julia
using Unitful: percent as pc
using Unitful: pertenthousand as bp

Percent = typeof(1pc)
BasisPoint = typeof(1bp)

struct mystruct
    rate::Percent
    spread::BasisPoint
end

mystruct(4pc, 12bp)

```

This is great!  
Though it wont allow 4.5pc.  
Steve’s longer approach allowed for this.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 12, 2024, 10:54pm UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/7 "2024-12-12T22:54:24Z")

</div>

It’s allowed:

```julia
julia> 4.5pc
4.5 %

```

Except in your context (but likely fixable):

> [@Lincoln\_Hannah](#):
>
> Though it wont allow 4.5pc.

I.e. that seem ~~s~~ ed intentional, only allowing integers there:

```julia
julia> mystruct(4.5pc, 12bp)
ERROR: InexactError: Int64(4.5)

```

I see it now, did you mean for:

```julia
julia> Percent = typeof(1.0pc)
Unitful.Quantity{Float64, NoDims, Unitful.FreeUnits{(%,), NoDims, nothing}}

julia> 4.5pc
4.5 %

julia> typeof(ans)
Unitful.Quantity{Float64, NoDims, Unitful.FreeUnits{(%,), NoDims, nothing}}

julia> 4pc
4 %

julia> typeof(ans) # beware not same:
Unitful.Quantity{Int64, NoDims, Unitful.FreeUnits{(%,), NoDims, nothing}}

```

There’s probably some easy way(?) to allow the latter, meaning to convert to the former type if that’s what you want (or stored as an integer scaled by 10, though a bit more work… yet more option is a decimal type…).

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 13, 2024, 1:36am UTC](https://discourse.julialang.org/t/percent-written-as-x/123735/8 "2024-12-13T01:36:53Z")

</div>

thank you. that works.

```julia
Percent = typeof(1.0pc)

```
