# Stringify number with unicode minus symbol

**URL:** <https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905>\
**Category:** New to Julia\
**Tags:** plots, unicode\
**Created:** [January 13, 2023, 5:49am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905 "2023-01-13T05:49:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [January 13, 2023, 5:49am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905/1 "2023-01-13T05:49:22Z")

</div>

This is a minor, simple question. Is there already a function that acts like `string()` but uses the unicode minus symbol for negative numbers? (It would be nice if `string()` accepted such an option.)

If not, I’d just write a one-line function like this

```julia
num2str(a) = (a >= 0) ? string(a) : "−" * string(-a)

```

I need this for annotations in plots.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [January 13, 2023, 9:48am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905/2 "2023-01-13T09:48:45Z")

</div>

how about

```julia
julia> num2str(a) = replace(string(a), '-' => '−')
num2str (generic function with 1 method)

julia> num2str(-2)
"−2"

```

possibly `r"^-" => '−'` if you want to ensure it’s the first character (though I can’t think of a case where this would matter).

Alternative in the vein of your function:

```julia
n2s(a) = "$('−'^(a < 0))$(abs(a))"

```

---

<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:** [January 13, 2023, 10:31am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905/3 "2023-01-13T10:31:04Z")

</div>

I cannot see the difference in Plots.jl gr backend, between the two minus characters.  
Need to use LaTeXStrings to see the difference:

```plaintext
using Plots, LaTeXStrings
num2str(a) = replace(string(a), '-' => L"−")
x = y = -7:7
plot(x,y, formatter = num2str)

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 13, 2023, 10:58am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905/4 "2023-01-13T10:58:05Z")

</div>

The difference depends on the font.

Here’s a scrappy diagram that’s mostly correct (fonts sometimes don’t want to play nicely):

 ![sampleline](https://global.discourse-cdn.com/julialang/original/3X/2/e/2eab3e07c1f78b25b02cfd1135e56a67d510590c.png)

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [January 16, 2023, 6:01am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905/5 "2023-01-16T06:01:31Z")

</div>

> I cannot see the difference in Plots.jl gr backend

On my system (julia 1.8.5 on macOS 13.1), I got the image at the bottom of this message from the code below. The difference between the hyphen and unicode minus is clear. Note that the numeric labels on the y axis (likely) uses the unicode minus symbol.

```julia
using Plots
m=-7
a = rand(Float64, 10) .- 0.5
plot(a;ylim=(-0.5,0.5))
annotate!(3, 0.1, "m=$(m)")
annotate!(3, -0.1, "m=−7")

```

 ![Screenshot 2023-01-16 at 14.58.15](https://global.discourse-cdn.com/julialang/original/3X/3/2/321bc3924aa603cf6ebb1e3934c9d7429d725fd4.png)

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [January 16, 2023, 6:12am UTC](https://discourse.julialang.org/t/stringify-number-with-unicode-minus-symbol/92905/6 "2023-01-16T06:12:53Z")

</div>

> possibly r"^-" =\> ‘−’ if you want to ensure it’s the first character

This comment of yours has made me realize that your first approach is better because of complex numbers:

```julia
c = 1 - 2im
plot( . . . )
annotate!(x, y, "c=$(c)") # uses hyphen
annotate!(x, y, "c=$(num2str(c))") # uses unicode minus

```

My sample code in my original post doesn’t work for complex numbers!
