# Converting Float to String with padding?

**URL:** <https://discourse.julialang.org/t/converting-float-to-string-with-padding/132865>\
**Category:** New to Julia\
**Created:** [October 3, 2025, 7:43pm UTC](https://discourse.julialang.org/t/converting-float-to-string-with-padding/132865 "2025-10-03T19:43:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quantodeluz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quantodeluz/32/218696_2.png) [@Quantodeluz](https://discourse.julialang.org/u/Quantodeluz)\
**Post date:** [October 3, 2025, 7:43pm UTC](https://discourse.julialang.org/t/converting-float-to-string-with-padding/132865/1 "2025-10-03T19:43:23Z")

</div>

Simple question here, how could I emulate the effect of `string(Int, pad=n)` with floats?  
Using `string` itself throws an error.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [October 3, 2025, 8:08pm UTC](https://discourse.julialang.org/t/converting-float-to-string-with-padding/132865/2 "2025-10-03T20:08:51Z")

</div>

You can use the [`Printf` standard library](https://docs.julialang.org/en/v1/stdlib/Printf/):

```julia-repl
julia> using Printf

julia> @sprintf("%10.4g", 3.14159) # 10 characters, 4 digits of precision, right-justified
" 3.142"

julia> @sprintf("%-10.4g", 3.14159) # 10 characters, 4 digits of precision, left-justified
"3.142 "

julia> Printf.format(Printf.format"%-10.4g", 3.14159) # equivalent to the previous
"3.142 "

```

See [printf, fprintf, sprintf, snprintf, printf\_s, fprintf\_s, sprintf\_s, snprintf\_s - cppreference.com](https://en.cppreference.com/w/c/io/fprintf) for more options. You can also use this library to get finer control of integer printing.

---

<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:** [October 3, 2025, 8:54pm UTC](https://discourse.julialang.org/t/converting-float-to-string-with-padding/132865/3 "2025-10-03T20:54:07Z")

</div>

An alternative, fwiw:

```julia-auto
julia> lpad(string(round(pi, digits=2)), 6)
" 3.14"

julia> lpad(string(round(pi, digits=2)), 6, '0')
"003.14"
```
