How to specify the format for printing an array of Floats in julia?

julia print format
julia print array
julia> round float
julia print vs println
example julia programs
julia print with color
julia set precision
formatting in julia

I have an array or matrix that I want to print, but only to three digits of precision. How do I do that. I tried the following.

> @printf("%.3f", rand())

> @printf("%.3f", rand(3))
LoadError: TypeError: non-boolean (Array{Bool,1}) used in boolean context 
while loading In[13], in expression starting on line 1

Update: Ideally, I just want to call a function like printx("{.3f}", rand(m, n)) without having to further process my array or matrix.

I would do it this way:

julia> map(x -> @sprintf("%.3f",x), rand(3))
3-element Array{String,1}:

printf, I would do it this way: julia> map(x -> @sprintf("%.3f",x), rand(3)) 3-element Array{​String,1}: "0.471" "0.252" "0.090". julia> float({big(pi)}) 1-element Array{Float64,1}: 3.14159 Since float depends on what you pass it, the best we can do is return another Any array with float applied to each element, but that's not too useful. Perhaps the fuzzy-typed functions like float and complex should require numeric arrays.

How about this?

julia> print(round.(rand(3); digits=3))

Printf · The Julia Language, Print args using C printf style format specification string, with some caveats: Inf %E , %f , %F , %g , and %G . Furthermore, if a floating point number is equally  Integers and Floating-Point Numbers. Integers and floating-point values are the basic building blocks of arithmetic and computation. Built-in representations of such values are called numeric primitives, while representations of integers and floating-point numbers as immediate values in code are known as numeric literals.

I don't think @printf accepts a list of arguments as you might be expecting.

One solution you could try it to use @sprintf to create formatted strings, but collect them up in a list comprehension. You might then use join to concatenate them together like so:

join([@sprintf "%3.2f" x for x in rand(3)], ", ")

Compact float printing in the REPL - Usage, How can I globally change the printing format of the REPL? I would specifically like to change the formatting of floats to a more compact style so that julia> 10/3 3.3333 instead of refers to :compact but I don't know how to set this. false) end julia> 2/3 0.667 julia> rand(10) 10-element Array{Float64,1}:  You can convert a 2x2 array of floats into a 2x2 array of ints very easily, after you decide how you want rounding to be handled: julia> A = [1.0 -0.3; 3.9 4.5] 2x2 Array{Float64,2}: 1.0 -0.3 3.9 4.5 julia> round.(Int, A) 2x2 Array{Int64,2}: 1 0 4 4 julia> floor.(Int, A) 2x2 Array{Int64,2}: 1 -1 3 4 julia> trunc.(Int, A) 2x2 Array{Int64,2}: 1 0 3 4 julia> ceil.(Int, A) 2x2 Array{Int64,2}: 1 0 4 5

The OP said:

Update: Ideally, I just want to call a function like printx("{.3f}", rand(m, n)) without having to further process my array or matrix.

This answer to a similar questions suggests something like this:

julia> VERSION
julia> using Printf

julia> m = 3; n = 5;  
julia> A = rand(m, n)
3×5 Array{Float64,2}:
 0.596055  0.0574471  0.122782  0.829356  0.226897
 0.606948  0.0312382  0.244186  0.356534  0.786589
 0.147872  0.61846    0.494186  0.970206  0.701587

# For this session of the REPL, redefine show function. Next REPL will be back to normal.    
# Note %1.3f% spec for printf format string to get 3 digits to right of decimal.
julia>, f::Float64) = @printf(io, "%1.3f", f)

# Now we have the 3 digits to the right spec working in the REPL.
julia> A
3×5 Array{Float64,2}:
 0.596  0.057  0.123  0.829  0.227
 0.607  0.031  0.244  0.357  0.787
 0.148  0.618  0.494  0.970  0.702

# The print function prints with 3 decimals as well, but note the semicolons for rows.
# This may not be what was wanted either, but could have a use.
julia> print(A)
[0.596 0.057 0.123 0.829 0.227; 0.607 0.031 0.244 0.357 0.787; 0.148 0.618 0.494 0.970 0.702]

Julia By Example, Notice that intelligence is applied to the process: if one of the elements looks like a floating-point number, for example, you'll get an array of Float64s: julia> a1  Subtype of IndexStyle used to describe arrays which are optimally indexed by one linear index. A linear indexing style uses one integer to describe the position in the array (even if it's a multidimensional array) and column-major ordering is used to access the elements. For example, if A were a (2,

Introducing Julia/print, output format for numbers (stand alone floats, float matrix, BigFloats). #If we want to specify the default output for plots generated by Plots.jl,  Julia Functions; Base.Cartesian; Talking to the compiler (the :meta mechanism) SubArrays; isbits Union Optimizations; System Image Building; Working with LLVM; printf() and stdio in the Julia runtime; Bounds checking; Proper maintenance and care of multi-threading locks; Arrays with custom indices; Module loading; Inference; Julia SSA-form IR

function to change numeric output format · Issue #6493 · JuliaLang , The difference is in the rounding behavior for display of a floating po. values correctly (no matter how you define the "correct" behaviour). trying to output decimal values, trying to do operations in a binary format is asking for trouble. julia> @printf "%.5e" f 1.01563e+00 julia> fill(f,2,2) 2×2 Array{Float64  I believe I have carefully read the entire printf() documentation but could not find any way to have it print out, say, the elements of a 10-element array of float(s). E.g., if I have float[] foo

Float printing with incorrect rounding. · Issue #33185 · JuliaLang , C/C++ if else statement with Examples · Jagged Array or Array of Arrays in C with Examples Between both, there can be elements governing the printing format. A number after % specifies the minimum field width to be printed if the integer, characters in string and number of digits after decimal part in floating value. I want change output format for numbers (stand alone floats, float matrix, BigFloats). I can do it in IPython by using %precision magic function. I didn't find any equivalent in Julia. I know t