# Float64 & format

**URL:** https://discourse.julialang.org/t/float64-format/110284
**Category:** General Usage
**Created:** [February 16, 2024, 9:21am UTC](https://discourse.julialang.org/t/float64-format/110284 "2024-02-16T09:21:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [February 16, 2024, 9:21am UTC](https://discourse.julialang.org/t/float64-format/110284/1 "2024-02-16T09:21:36Z")

</div>

How can I make Floats appear in scientific or engineering notation? E.g., I want to display, say, `0.0001` as `1e-4`.

I know I can do this using the `@printf` macro, but that just prints the formatted number to the terminal. I need to make it into a string.

How do I format Floats and produce a _string_?

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [February 16, 2024, 9:28am UTC](https://discourse.julialang.org/t/float64-format/110284/2 "2024-02-16T09:28:52Z")

</div>

You can use `@sprintf`, it’s like `@printf` but gives the string as return value.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [February 16, 2024, 9:38am UTC](https://discourse.julialang.org/t/float64-format/110284/3 "2024-02-16T09:38:31Z")

</div>

Thanks, that was simple 🙂

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 16, 2024, 3:37pm UTC](https://discourse.julialang.org/t/float64-format/110284/4 "2024-02-16T15:37:02Z")

</div>

You can also use the [JuliaString/Format.jl](https://github.com/JuliaString/Format.jl) package, which has a lot more formatting options (and some more bug fixes).

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [February 16, 2024, 8:13pm UTC](https://discourse.julialang.org/t/float64-format/110284/5 "2024-02-16T20:13:20Z")

</div>

I noticed the upgrade from `Formatting.jl` to `Format.jl`, and took a look at it. The documentation seems very rudimentary, but perhaps it is assumed that one checks the documentation of the related Python package?

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 17, 2024, 12:25am UTC](https://discourse.julialang.org/t/float64-format/110284/6 "2024-02-17T00:25:50Z")

</div>

I inherited the documentation from Formatting.jl - and yes, it mostly has the differences documented, where it doesn’t match the Python format function, or C’s printf (for the `cfmt` function).  
If you feel something needs to be better documented, or there is a difference that needs to be mentioned, please make an issue (or better yet, a PR 😁 )

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [February 17, 2024, 7:57am UTC](https://discourse.julialang.org/t/float64-format/110284/7 "2024-02-17T07:57:35Z")

</div>

I don’t have background from the C-world (i.e., I’m very shaky on printf/sprintf), and I haven’t used the Python system much for formatting (and very little since 2017…).

What was not clear to me from the Format.jl documentation was… how do I use it? If it is meant to be used in conjunction with printf, I didn’t see that mentioned or exemplified in my quick glancing through the description.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 17, 2024, 1:13pm UTC](https://discourse.julialang.org/t/float64-format/110284/8 "2024-02-17T13:13:51Z")

</div>

You wouldn’t use it with `@printf` or `@sprintf`, but more to replace using `@sprintf`.  
If you need to output, instead of building a formatted string, you can simply print it with `print`, i.e. `print(pyfmt("^8d", num))' or `print(cfmt(“%10.10”, str)`.

Instead of using Format.jl directly, you could also use [JuliaString/StrFormat.jl](https://github.com/JuliaString/StrFormat.jl), i.e. `f"This is a C formatted number \%10.3f(num), and this is a Python style format number \{^10.3e}(num)"` to create a string, and `pr"This prints the formatted string out, like @printf, for example a string truncated to 8 characters %8.8s(str)"
