# Avoid \`-\` in \`@sprintf("%.1f", -0.001)\`

**URL:** https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334
**Category:** General Usage
**Tags:** question, printf
**Created:** [May 29, 2026, 11:23am UTC](https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334 "2026-05-29T11:23:13Z")
**Posts on this page:** 5
**Page:** 1

<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: [May 29, 2026, 11:23am UTC](https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334/1 "2026-05-29T11:23:13Z")

</div>

The title says it all, but generally I want to avoid a negative number that is rounded to all-zeros by `printf` to have a `-`.

```julia
julia> using Printf

julia> @sprintf("%.1f", -0.001)
"-0.0"

```

> **see a truly horrid solution here**
>
> ```julia-auto
> function my_format(x::Real)
> s = @sprintf("%.1f", x)
> m = match(r"^-([0.]+)$", s)
> if m ≡ nothing
> s
> else
> m.captures[1]
> end
> end
> 
> ```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 29, 2026, 11:38am UTC](https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334/2 "2026-05-29T11:38:15Z")

</div>

AFAIK `printf` doesn’t support this. Something like `x = round(x; digits = 1) == 0 ? 0 : x` seems slightly cleaner than tweaking the output, especially when your format string specifies padding etc.

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [May 29, 2026, 2:19pm UTC](https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334/3 "2026-05-29T14:19:25Z")

</div>

This is standard IEEE 754 floating-point behavior, and there are three workarounds,

Along the lines as suggested by @pfitzseb

```julia
using Printf

val = -0.001
# If the value rounds to 0.0, use the absolute value
formatted = @sprintf("%.1f", abs(val) < 0.05 ? 0.0 : val)

```

or a slightly less horrid function

```julia
using Printf

val = -0.001

# Add 0.0 to strip the negative sign from -0.0
# round(val, digits=1) results in -0.0, but -0.0 + 0.0 == 0.0
clean_val = round(val, digits=1) + 0.0

@sprintf("%.1f", clean_val) # Output: "0.0"

```

I’d avoid the only other alternative I see

```julia
replace(@sprintf("%.1f", -0.001), "-0.0" => "0.0")

```

---

<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: [May 29, 2026, 2:43pm UTC](https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334/4 "2026-05-29T14:43:05Z")

</div>

> [@technocrat](#):
>
> This is standard IEEE 754 floating-point behavior

That may well be, but it does not make sense in my context (generating tables for a research paper). In that context, the `-0.0` is an unnecessary distraction.

I wish there was a package that would simply decompose a (binary) float to a bunch of digits (dumped into a `Vector{UInt8}` or similar), a decimal exponent, and a sign, then let the user deal with that for further processing. Of course the [_internals_ of Ryu in Base](https://github.com/JuliaLang/julia/tree/2045dc97e05c3c241fb0c4b29c211e6a404ee399/base/ryu) do exactly that, but it is not part of the API and has a different API.

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [May 29, 2026, 8:00pm UTC](https://discourse.julialang.org/t/avoid-in-sprintf-1f-0-001/137334/5 "2026-05-29T20:00:44Z")

</div>

Of course, but the standard isn’t for strings. You’re right, though, that its a PITA when you have to go from a float to a string representation. I like your idea of decomposing.
