# Error while formatting a specific number!

**URL:** https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172
**Category:** New to Julia
**Tags:** numbers, formatting
**Created:** [July 6, 2021, 7:46pm UTC](https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172 "2021-07-06T19:46:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![esmith289](https://avatars.discourse-cdn.com/v4/letter/e/87869e/32.png) [@esmith289](https://discourse.julialang.org/u/esmith289)
#### Post date: [July 6, 2021, 7:46pm UTC](https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172/1 "2021-07-06T19:46:57Z")

</div>

This seems total crazy to me:

so in the REPL I write:

julia\> using Formatting  
julia\> ff= string(“{:.e”,4,“E”,“}”}  
“{:.4e}”

Then I use ff to format a number like:  
julia\> zz = Formatting.format(ff,1E-8)  
“1.0000e-08” # correct

and,  
julia\> zz = Formatting.format(ff,2E-8)  
“2.0000e-08” # correct

BUT, for the specific number 3E-8

I get:  
julia\> zz = Formatting.format(ff,3.0E-8)  
“2.10000E-08” # WRONG!!

BUT  
julia\> zz = Formatting.format(ff,3.0E-9)  
“3.0000E-09” # correct

So please tell my that I’m doing something stupid and this is not a bug. Works the same way in a .jl script. Version 1.6.1 (2021-04-23)

---

<div class="post-metadata">

### Author: ![esmith289](https://avatars.discourse-cdn.com/v4/letter/e/87869e/32.png) [@esmith289](https://discourse.julialang.org/u/esmith289)
#### Post date: [July 6, 2021, 7:58pm UTC](https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172/2 "2021-07-06T19:58:42Z")

</div>

zz = Formatting.format(ff,30.0E-9)  
“2.10000E-08” # WRONG!!

Also gives the same erroneous result.

---

<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: [July 6, 2021, 8:03pm UTC](https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172/3 "2021-07-06T20:03:51Z")

</div>

There seem to be some longstanding bugs in the floating-point formatting provided by the Formatting.jl package (e.g. [printfmt("1:.4e, 40000.0) prints 3.10000e+04 · Issue #71 · JuliaIO/Formatting.jl · GitHub](https://github.com/JuliaIO/Formatting.jl/issues/71)).

I would tend to recommend using Julia’s [`Printf` standard library](https://docs.julialang.org/en/v1/stdlib/Printf/) instead:

```nohighlight
julia> using Printf

julia> @sprintf("%0.4E", 3.0e-8)
"3.0000E-08"

```

PS. Please [quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

---

<div class="post-metadata">

### Author: ![esmith289](https://avatars.discourse-cdn.com/v4/letter/e/87869e/32.png) [@esmith289](https://discourse.julialang.org/u/esmith289)
#### Post date: [July 6, 2021, 8:39pm UTC](https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172/4 "2021-07-06T20:39:56Z")

</div>

Ok thanks. The reason I used Formatting.format is because I need the actual formatting string to be a variable since in my use case I am change the significant digits (e.g. might be “{:.5e}” vs. “{:.4e}” ) and @sprintf can’t handle a variable for the string. Guess I’'ll try a different approach.

---

<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: [July 6, 2021, 11:03pm UTC](https://discourse.julialang.org/t/error-while-formatting-a-specific-number/64172/5 "2021-07-06T23:03:49Z")

</div>

> [@esmith289](#):
>
> I need the actual formatting string to be a variable since in my use case I am change the significant digits

This should be supported soon in Printf ([adding dynamic width and precision to printf by johanmon · Pull Request #40105 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/40105)) but won’t be in an official release until Julia 1.8, unfortunately.
