# Print boolean with PrettyTables.jl

**URL:** https://discourse.julialang.org/t/print-boolean-with-prettytables-jl/65491
**Category:** General Usage
**Tags:** question, tables, io, prettytables
**Created:** [July 29, 2021, 11:51am UTC](https://discourse.julialang.org/t/print-boolean-with-prettytables-jl/65491 "2021-07-29T11:51:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [July 29, 2021, 11:51am UTC](https://discourse.julialang.org/t/print-boolean-with-prettytables-jl/65491/1 "2021-07-29T11:51:51Z")

</div>

I am printing a struct with PrettyTables.jl and would like boolean values to print out as true or false rather than Float64s. Is there a way to do this? Thank you.

**Example**

```julia
using PrettyTables

struct M
    x::String
    y::Float64
    z::Bool
end

function Base.show(io::IO, ::MIME"text/plain", m::M)
    values = [getfield(m, f) for f in fieldnames(M)]
    return pretty_table(
        values;
        compact_printing=false,
        header=["Value"],
        row_name_alignment=:l,
        row_names=[fieldnames(M)...],
        formatters=ft_printf("%5.2f"),
        alignment=:l,
    )
end

m = M("x", .3, true)

```

**Output:**

```julia
julia> m
┌───┬───────┐
│ │ Value │
├───┼───────┤
│ x │ x │
│ y │ 0.30 │
│ z │ 1.00 │
└───┴───────┘

```

---

<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: [July 29, 2021, 12:15pm UTC](https://discourse.julialang.org/t/print-boolean-with-prettytables-jl/65491/2 "2021-07-29T12:15:45Z")

</div>

Converting boolean to string before returning the pretty\_table, seems to work:

```julia
 [(typeof(v) == Bool) && (values[i] = string(v)) for (i,v) in pairs(values)]

```

the output is:

```julia
julia> m = M("x", .3, true)
┌───┬───────┐
│ │ Value │
├───┼───────┤
│ x │ x │
│ y │ 0.30 │
│ z │ true │
└───┴───────┘

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 29, 2021, 12:45pm UTC](https://discourse.julialang.org/t/print-boolean-with-prettytables-jl/65491/3 "2021-07-29T12:45:18Z")

</div>

It’s the `formatters` option. I think it applies to all `<: Number` types, and `Bool <: Number`.

You can define a small helper function which checks if something is a `Bool` and if so, returns that thing, and if not uses `@sprintf`, like in [this thread](https://discourse.julialang.org/t/converting-a-double-number-into-a-string-not-printing/65492).

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [July 29, 2021, 12:46pm UTC](https://discourse.julialang.org/t/print-boolean-with-prettytables-jl/65491/4 "2021-07-29T12:46:07Z")

</div>

Thank you!
