# @printf: significant digits in floating point representation

**URL:** <https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978>\
**Category:** General Usage\
**Tags:** question, latex, formatting, printf\
**Created:** [October 16, 2019, 9:54am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978 "2019-10-16T09:54:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 16, 2019, 9:54am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/1 "2019-10-16T09:54:12Z")

</div>

hi i want tu use `@sprintf` to format a number to floating point representation with a fixed amount of significant digits  
how can I do that?  
The function shout act the following way:

```julia
.000012340000 -> 0.000012
.123400000000 -> 0.12
1.234 -> 1.2
1234000.000 -> 1200000

```

best…

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 16, 2019, 9:57am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/2 "2019-10-16T09:57:13Z")

</div>

edit: misread your question, you want significant digits not decimal digits

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 16, 2019, 10:03am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/4 "2019-10-16T10:03:15Z")

</div>

I think `%.3e` or `%.3g` may be what you’re looking for, which gives scientific notation

> **[printf | Type field](https://en.wikipedia.org/wiki/Printf_format_string#Type_field)**
>
> The Type field can be any of:

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 16, 2019, 10:05am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/5 "2019-10-16T10:05:32Z")

</div>

…but the problem is, that i want to use this to throw the number against Tikz / PGFplotsX. but LaTeX can not deal with scientific notation very well.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 16, 2019, 10:06am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/6 "2019-10-16T10:06:13Z")

</div>

as a last resort, you can always get the string and manipulate that

edit: actually, should have looked at `round` first  
[https://github.com/JuliaLang/julia/issues/21101#issuecomment-446958508](https://github.com/JuliaLang/julia/issues/21101#issuecomment-446958508)

```julia
julia> round(pi, digits=3)
3.142

julia> round(pi, sigdigits=3)
3.14

```

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 16, 2019, 10:09am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/7 "2019-10-16T10:09:55Z")

</div>

Hahaha…[. yes this i also had i mind but](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/20) and the thread ended up in [this issue](https://github.com/JuliaLang/julia/issues/33428)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 16, 2019, 10:10am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/8 "2019-10-16T10:10:53Z")

</div>

but now:

```julia
julia> round(.000012340000, sigdigits=3)
1.23e-5
julia> @printf "%.f" round(.000012340000, sigdigits=3)
0.000012

```

well this is slightly wrong too…um, yea just be a string wrangler for now I guess, sorry bud

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 16, 2019, 10:14am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/9 "2019-10-16T10:14:37Z")

</div>

> [@jling](#):
>
> well this is slightly wrong too…um, yea just be a string wrangler for now I guess, sorry bud

I feared that answer 😢  
thanks for now… still hoping for a better solution…

---

<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:** [February 4, 2024, 10:53pm UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/10 "2024-02-04T22:53:27Z")

</div>

Fyi, you might want to check out [this MATLAB solution](https://www.mathworks.com/matlabcentral/answers/379814-is-there-an-easy-way-to-format-numbers-to-3-significant-figures#answer_686890).

Below is a simplified Julia version that produces the desired output:

```julia
using Printf

function str_significant(x::Float64, sigdig::Int)
    (x == 0) && (return "0")
    x = round(x, sigdigits=sigdig)
    n = length(@sprintf("%d", abs(x))) # length of the integer part
    if (x ≤ -1 || x ≥ 1)
        decimals = max(sigdig - n, 0) # 'sig - n' decimals needed 
    else
        Nzeros = ceil(Int, -log10(abs(x))) - 1 # No. zeros after decimal point before first number
        decimals = sigdig + Nzeros
    end 
    return @sprintf("%.*f", decimals, x)
end

str_significant(.000012340000, 2) # 0.000012
str_significant(.123400000000, 2) # 0.12
str_significant(1.234, 2) # 1.2
str_significant(1234000.000, 2) # 1200000

```

---

<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:** [February 4, 2024, 11:04pm UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/11 "2024-02-04T23:04:59Z")

</div>

> [@MatFi](#):
>
> but LaTeX can not deal with scientific notation very well

Can’t you use a simple regex to change `e+XXX` to `\times {10}^{XXX}` for LaTeX?

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [February 5, 2024, 12:15am UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/12 "2024-02-05T00:15:58Z")

</div>

Is there a reason that you can’t use [siunitx](https://texdoc.org/serve/siunitx/0), the LateX package that is designed for this? In my code to automatically generate tables, I have lots of `@printf "\\num{%4.3e} \\n` and so on.

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [March 9, 2024, 8:57pm UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/13 "2024-03-09T20:57:05Z")

</div>

Nice, thanks  
this problem however is that old, that I can’t remember the final use case anymore. 🥴 (and the GitHub issue is still open 🙂

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [March 9, 2024, 8:58pm UTC](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/14 "2024-03-09T20:58:32Z")

</div>

I’m quite sure there was a reason I forgot about… sorry 🙄
