# How to define verbose output (for a polynomial)?

**URL:** <https://discourse.julialang.org/t/how-to-define-verbose-output-for-a-polynomial/21317>\
**Category:** New to Julia\
**Created:** [February 28, 2019, 7:43pm UTC](https://discourse.julialang.org/t/how-to-define-verbose-output-for-a-polynomial/21317 "2019-02-28T19:43:41Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [February 28, 2019, 8:18pm UTC](https://discourse.julialang.org/t/how-to-define-verbose-output-for-a-polynomial/21317/5 "2019-02-28T20:18:47Z")

</div>

```julia
function Base.show(io::IO, p::Poly)
    coeffs = p.coeffs
    print(io, "x^", length(coeffs))
    for (i, c) in enumerate(reverse(coeffs))
        c == 0 && continue
        
        ex = length(coeffs) - i
        print(io, ' ', c > 0 ? '+' : '-', ' ')
        print(io, abs(round(c, sigdigits=2)))
        ex > 0 && print(io, 'x')
        ex > 1 && print(io, '^', ex)
    end
end

```

```julia
julia> Poly(rand(10).-0.5)
x^10 - 0.5x^9 - 0.47x^8 - 0.41x^7 + 0.16x^6 - 0.3x^5 - 0.14x^4 + 0.17x^3 + 0.25x^2 - 0.0077x + 0.19

julia> Poly(rand(10).^rand(10).-0.5)
x^10 - 0.17x^9 + 0.47x^8 + 0.49x^7 - 0.057x^6 - 0.17x^5 + 0.44x^4 + 0.27x^3 - 0.25x^2 + 0.041x - 0.09

julia> Poly(rand(10).-0.5)
x^10 + 0.2x^9 - 0.36x^8 + 0.0081x^7 + 0.12x^6 - 0.31x^5 + 0.37x^4 + 0.43x^3 - 0.093x^2 - 0.37x - 0.29

julia> Poly([1,-2,-3.165])
x^3 - 3.2x^2 - 2.0x + 1.0

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-define-verbose-output-for-a-polynomial/21317)._
