# Print vs @printf

**URL:** <https://discourse.julialang.org/t/print-vs-printf/121272>\
**Category:** General Usage\
**Tags:** printf, print\
**Created:** [October 14, 2024, 6:13am UTC](https://discourse.julialang.org/t/print-vs-printf/121272 "2024-10-14T06:13:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [October 14, 2024, 6:13am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/1 "2024-10-14T06:13:34Z")

</div>

Hello,  
Is it possible to write the following program using print, round and digits instead of using @printf?

```julia
using Printf
# needed in order to use @printf
function tickets()
   print("Price of reserved ticket? ")
   rPrice = parse(Float64, readline())
   print("Number sold? ")
   rTickets = parse(Int, readline())
   rSales = rPrice * rTickets
   print("Price of stands ticket? ")
   sPrice = parse(Float64, readline())
   print("Number sold? ")
   sTickets = parse(Int, readline())
   sSales = sPrice * sTickets
   print("Price of grounds ticket? ")
   gPrice = parse(Float64, readline())
   print("Number sold? ")
   gTickets = parse(Int, readline())
   gSales = gPrice * gTickets
   tTickets = rTickets + sTickets + gTickets
   tSales = rSales + sSales + gSales
   @printf("\nReserved sales: \$%0.2f\n", rSales)
   @printf("Stands sales: \$%0.2f\n", sSales)
   @printf("Grounds sales: \$%0.2f\n", gSales)
   @printf("\nTotal tickets sold: %d\n", tTickets)
   @printf("Total money collected: \$%0.2f\n", tSales)
end
tickets()

```

This is an example from the book **Julia - Bit by Bit: Programming for Beginners**. In this book, the author says:

> As explained earlier, we use @printf (rather than print ) to ensure the dollar amounts are always printed to two decimal places.

Thank you.

---

<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 14, 2024, 6:58am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/3 "2024-10-14T06:58:09Z")

</div>

One simple example:

```julia
rSales = 1414.217

using Printf
@printf("\nReserved sales: \$%0.2f\n", rSales)

print("\nReserved sales: \$$(round(rSales, digits=2))\n")

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 14, 2024, 8:40am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/4 "2024-10-14T08:40:34Z")

</div>

While as @rafael.guerra shows, the answer is _yes_, if you don’t like the `@printf` syntax, I would suggest using Format.jl instead of basically implementing it yourself (though this can of course be useful to learn the language).

```julia-repl
julia> using Format

julia> rSales = 1414.217;

julia> println("\nReserved sales: $(format(rSales, precision=2))") # (Note: format outputs a String)

Reserved sales: 1414.22

```

In this case it is not easier than just `round`ing, but it is more flexible. E.g. if you want to add a thousands separator `,`, you can simply use

```julia-repl
julia> println("\nReserved sales: $(format(rSales, precision=2, commas=true))") # (Note: only comma as thousands separator is supported)

Reserved sales: 1,414.22

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [October 14, 2024, 9:27am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/5 "2024-10-14T09:27:29Z")

</div>

Hi,  
Thank you so much.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [October 15, 2024, 6:39am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/6 "2024-10-15T06:39:07Z")

</div>

Hello,  
In book I read a line like below:

> In general, @printf allows us to have more control over our output format than we have with print / println .

Is it true?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 15, 2024, 7:09am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/7 "2024-10-15T07:09:13Z")

</div>

Sure, just take a look at the examples in the documentation of `@printf` (via `?@printf` in the REPL). Most of these would be a hassle to achieve via `print`.

On the other hand, `print` (and `show`) work for more complicated objects. E.g. you can easily (pretty) print a Matrix `M` (`print(M)` or `show(stdout, "text/plain", M`), but for `@printf` this would require looping and taking care of the alignment yourself.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [October 26, 2024, 7:51am UTC](https://discourse.julialang.org/t/print-vs-printf/121272/8 "2024-10-26T07:51:40Z")

</div>

Hi,  
Thank you so much.
