# Make custom type behave like Float64

**URL:** <https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926>\
**Category:** General Usage\
**Tags:** struct, interface, show\
**Created:** [October 6, 2025, 8:32pm UTC](https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926 "2025-10-06T20:32:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 6, 2025, 8:32pm UTC](https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926/1 "2025-10-06T20:32:52Z")

</div>

(Related to [Packages for colored REPL output?](https://discourse.julialang.org/t/packages-for-colored-repl-output/132925).)

If I make a function return a custom type so that I can create a formatted `show` method for it, what functions do I then need to extend to make my type behave like Float64 everywhere? I don’t see anything like that listed in [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/).

```julia-auto
using Format
struct PercentError
    x::Float64
end
Base.show(io::IO, x::PercentError) = print(io, format(x.x, commas=true, precision=3, suffix=" %"))

```

```julia-auto
julia> PercentError(0.23456)
0.235 %

julia> PercentError(0.23456) + 3
ERROR: MethodError: no method matching +(::PercentError, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 6, 2025, 8:42pm UTC](https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926/2 "2025-10-06T20:42:55Z")

</div>

This is definitely an X-Y problem because defining your own number type is almost certainly overkill for whatever printing problem you face. You can see the implementation of [Dual Numbers](https://github.com/JuliaDiff/DualNumbers.jl/blob/master/src/dual.jl) as an example, but in general implementing your own number type just to print things easier is a lot.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 6, 2025, 9:47pm UTC](https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926/3 "2025-10-06T21:47:35Z")

</div>

It really seems like a `PercentError` storing a `Float64` that way shouldn’t be treated like one. Scaling by other `Real` numbers could reasonably result in another `PercentError`. However, scaling by another `PercentError` is a problem: 1% stores `1.0`, but 1%^2 is 0.01%, which stores `0.01`, not `1.0^2 == 1.0`.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 7, 2025, 2:10pm UTC](https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926/4 "2025-10-07T14:10:52Z")

</div>

> [@pdeffebach](#):
>
> This is definitely an X-Y problem because defining your own number type is almost certainly overkill for whatever printing problem you face.

Yeah, I thought this may be a potential solution for the printing issue in the linked thread. I guess not. I want to remind myself that my function output already multiplied by 100 to convert to percent. I could `print` the answer formatted at the end of the function, but it will still return the raw answer right below that unless I remember to suppress with `;`.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 7, 2025, 2:12pm UTC](https://discourse.julialang.org/t/make-custom-type-behave-like-float64/132926/5 "2025-10-07T14:12:31Z")

</div>

Good point. I probably need to store the number as a decimal and only print it as %.
