# Convert float to string with given decimal points

**URL:** <https://discourse.julialang.org/t/convert-float-to-string-with-given-decimal-points/88775>\
**Category:** General Usage\
**Tags:** question, plotting, formatting\
**Created:** [October 15, 2022, 12:15pm UTC](https://discourse.julialang.org/t/convert-float-to-string-with-given-decimal-points/88775 "2022-10-15T12:15:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lieskjur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lieskjur/32/27611_2.png) [@lieskjur](https://discourse.julialang.org/u/lieskjur)\
**Post date:** [October 15, 2022, 12:15pm UTC](https://discourse.julialang.org/t/convert-float-to-string-with-given-decimal-points/88775/1 "2022-10-15T12:15:58Z")

</div>

I am making some animations with the `@animate` macro and would like to display time in the title.

Right now I am using `string(round(t,digits=2))` but whenever the converted number has less than two decimals the text gets re-aligned resulting in a jittering effect.

![animation](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a6de2a5cb0e226b8f09234b033e75baa0e1a02c.gif)

The command `@printf("%.2",1)` produces the desired output but sends it to the IO instead of a variable.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 15, 2022, 12:19pm UTC](https://discourse.julialang.org/t/convert-float-to-string-with-given-decimal-points/88775/2 "2022-10-15T12:19:00Z")

</div>

sprintf should do what you want.

---

<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:** [October 15, 2022, 12:19pm UTC](https://discourse.julialang.org/t/convert-float-to-string-with-given-decimal-points/88775/3 "2022-10-15T12:19:25Z")

</div>

> [@lieskjur](#):
>
> The command `@printf("%.2",1)` produces the desired output but sends it to the IO instead of a variable.

See [`@sprintf`](https://docs.julialang.org/en/v1/stdlib/Printf/#Printf.@sprintf).

---

<div class="post-metadata">

**Author:** ![lieskjur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lieskjur/32/27611_2.png) [@lieskjur](https://discourse.julialang.org/u/lieskjur)\
**Post date:** [October 15, 2022, 12:21pm UTC](https://discourse.julialang.org/t/convert-float-to-string-with-given-decimal-points/88775/4 "2022-10-15T12:21:05Z")

</div>

Does indeed. Thank you very much.
