Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round everything to 4 decimal places

Tags:

julia

Is there global setting to round everything that's printed to the terminal in Julia to 4 decimal places?

Any print - float, arrays, matrices, dataframes, structures - if it has float it should be printed with 4 digits after decimal point, like 2.0000.

like image 381
Alex Craft Avatar asked Dec 14 '25 03:12

Alex Craft


2 Answers

You could try by overriding the Base.show() method in julia like so:

import Printf

function Base.show(io::IO, f::Float64)
    Printf.@printf(io, "%.4f", f)
end
    

After executing this code, any Float64 value printed to the terminal will be rounded to four decimal places.

like image 76
Simon Avatar answered Dec 16 '25 22:12

Simon


Try this:


using Printf
format_float(x) = @sprintf("%.4f", x)
function print_formatted(data)
    if isa(data, AbstractArray)
        for row in eachrow(data)
            println(join([format_float(x) for x in row], "\t"))
        end
    else
        println(format_float(data))
    end
end
like image 38
Ranadip Dutta Avatar answered Dec 16 '25 23:12

Ranadip Dutta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!