0

How can I limit the digits of data matrix to 2 digits?

data = reshape([tuple(c[i], c2[i]) for i in eachindex(c, c2)], 9, 9)
#9×9 Matrix{Tuple{Real, Real}}:


hl = Highlighter((d,i,j)->d[i,j][1]*d[i,j][2] < 0, crayon"red")
pretty_table(data ; header = names, row_names= names , highlighters=hl)

enter image description here

August
  • 12,410
  • 3
  • 35
  • 51
Hana
  • 101
  • 5

4 Answers4

0

There are a couple of ways to do this:

  1. You can use the round function:

data = round(data, 2)

  1. You can use string formatting:

data = ["{0:.2f}".format(x) for x in data]

James
  • 144
  • 6
0

You can use an anonymous function as a formatter, like this:

formatter = (v, i, j) -> round(v, digits=2);

hl = Highlighter((d,i,j)->di,j*d[i,j][2] < 0, crayon"red")

pretty_table(data; header=names, row_names=names , highlighters=hl, formatters=formatter)

I encourage you to read the documentations for further options.

Shayan
  • 5,165
  • 4
  • 16
  • 45
0

You can round the numbers when creating the data variable:

round2(n) = round(n; digits = 2)
data = reshape([tuple(round2(c[i]), round2(c2[i])) for i in eachindex(c, c2)], 9, 9)

Or if you want to maintain precision in the data array, but limit to 2 digits just for the printing, you can use the formatters keyword argument of PrettyTables:

pretty_table(data; header = titles, row_names = titles, formatters = (v, i, j) -> round.(v; digits = 2))
Sundar R
  • 13,776
  • 6
  • 49
  • 76
0

I understand you have a matrix of tuples such as:

julia> mx = [tuple(rand(2)...) for i in 1:3,  j=1:2]
3×2 Matrix{Tuple{Float64, Float64}}:
 (0.617653, 0.0742714)  (0.0824311, 0.0344668)
 (0.327074, 0.235599)   (0.912262, 0.0250492)
 (0.116079, 0.387601)   (0.804606, 0.81485)

This can be rounded as:

julia> (x->round.(x;digits=2)).(mx)
3×2 Matrix{Tuple{Float64, Float64}}:
 (0.62, 0.07)  (0.08, 0.03)
 (0.33, 0.24)  (0.91, 0.03)
 (0.12, 0.39)  (0.8, 0.81)
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62