# Round a float, and force a specific number of decimal places

**URL:** <https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547>\
**Category:** New to Julia\
**Tags:** numbers, formatting\
**Created:** [September 21, 2022, 7:22am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547 "2022-09-21T07:22:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [September 21, 2022, 7:22am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/1 "2022-09-21T07:22:15Z")

</div>

Rounding a float to two decimal places, say, does not necessarily create a number with two decimal places. For example

```julia
round(10.001, digits = 2)

```

produces 10.0

How can I force a specific number of shown decimal places?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [September 21, 2022, 7:41am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/2 "2022-09-21T07:41:32Z")

</div>

What is displayed and what it is rounded to is not the same thing, similar to your example we also have

```julia-repl
julia> round(10.1, sigdigits=2)
10.0

```

where it is correctly rounded to two significant digits, but since the following decimals are zero it is still displayed in standard float format with a single decimal zero to signify that it is a float with an integer value (I guess that is the reason at least).

So if you want to change the way that is shown as a result I guess you have to override the `show` method for floats.

If you just want to print it in a specific way you could have a look at `@printf`, see e.g.

> [@How to format float numbers in human-friendly format?](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/2):
>
> I don’t know how “noisy” it is for you, but the command for versatile printing is [printf](https://docs.julialang.org/en/v1/stdlib/Printf/), which by the way is a lot faster than before in Julia 1.6 In this case, the command would be: @printf "my number: %.2f" f

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [September 21, 2022, 8:24am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/3 "2022-09-21T08:24:44Z")

</div>

Just adding that `@printf` is in the `Printf` module from standard library, so `using Printf` is needed. Also `@sprintf` is useful to get a string with formatted output.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 21, 2022, 8:28am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/4 "2022-09-21T08:28:51Z")

</div>

Format.jl and Formatting.jl are alternatives to Printf with a more modern formatting language.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 21, 2022, 9:03am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/5 "2022-09-21T09:03:13Z")

</div>

> [@Nash](#):
>
> produces 10.0

Put very simply, it’s because

```julia
julia> 10.00
10.0

```

---

<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:** [September 21, 2022, 11:01am UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/6 "2022-09-21T11:01:28Z")

</div>

Check [this post](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/3) for a Matlab-like solution.

---

<div class="post-metadata">

**Author:** ![deszoeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deszoeke/32/2174_2.png) [@deszoeke](https://discourse.julialang.org/u/deszoeke)\
**Post date:** [May 2, 2025, 6:36pm UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/7 "2025-05-02T18:36:14Z")

</div>

```julia
**julia>** round(0.79598037, sigdigits=2)

0.8

**julia>** round(0.79598037, sigdigits=3)

0.796

```

The correct 2-sigdigit answer is “0.80”

---

<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:** [May 2, 2025, 6:42pm UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/8 "2025-05-02T18:42:49Z")

</div>

> [@deszoeke](#):
>
> The correct 2-sigdigit answer is “0.80”

I think you’re just confused by how it displays. The `round` function tries to round it to the closest `Float64` value to the one requested, but then has no control over how it is displayed — it is printed in the shortest possible decimal representation, like any other `Float64` value.

Moreover, since this is still a **binary** floating-point value (= integer times power of **two** ), it isn’t possible for it to be an exact decimal value anyway. e.g. the `Float64` value `0.8` is actually `0.8000000000000000444089209850062616169452667236328125`, which is the closest 53-bit integer × 2ᵏ value to `0.8`. (This is just how computer arithmetic works; it’s not specific to Julia.)

If you just want to _print_ something to two significant digits, use `@printf` or `@sprintf` from the `Printf` standard library:

```julia
julia> @printf("%0.2f", 0.79598037) # two digits after the decimal point
0.80
julia> @printf("%0.1e", 0.79598037) # two significant digits in scientific notation
8.0e-01

```

---

<div class="post-metadata">

**Author:** ![deszoeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deszoeke/32/2174_2.png) [@deszoeke](https://discourse.julialang.org/u/deszoeke)\
**Post date:** [May 2, 2025, 8:28pm UTC](https://discourse.julialang.org/t/round-a-float-and-force-a-specific-number-of-decimal-places/87547/9 "2025-05-02T20:28:23Z")

</div>

Of course!

The keyword `sigdigit` of round, referring to decimal digits confused me.

In this context, the OP was asking for a “specific number of /shown/ decimal places”. As said above, this is a problem of displaying strings for the likes of `printf`, not `round` for rounding the numeric type.

Though not in `Base`, `Printf` is a standard library.
