5
$\begingroup$

V 12.1 on windows.

I do not remember if I've seen this and googling is not helping.

I wanted to format a numerical matrix for display using MatrixForm. But first I wanted to apply NumberForm on each entry, because I wanted all entries to have same width when displayed (so they do not shift around during display and running.

But NumberForm does not work well with MatrixForm. Is there a way around this?

a = {{1.3432, 34.432123}, {5.65454, 78.9883}}
MatrixForm[a]

Mathematica graphics

And now

a = {{1.3432, 34.432123}, {5.65454, 78.9883}};
b = NumberForm[a, {2, 2}]

Mathematica graphics

And it is the above I want to display in MatrixForm But it does not work on it due to the NumberForm wrapper in between

 MatrixForm[b]

Mathematica graphics

Also doing things in reverse ofcourse does not work either

 NumberForm@MatrixForm[a]

I could ofcourse use Round on the matrix first, then apply MatrixForm but I prefer to use things like NumberForm and AccountingForm since they allow more control on the format.

Is there a better way to do the above?

$\endgroup$
2
  • 1
    $\begingroup$ This is a little less convenient that xzczd's solution, so I'm just leaving this as a comment: Map[NumberForm[#, {2, 2}] &, {{1.3432, 34.432123}, {5.65454, 78.9883}}, {2}] // MatrixForm $\endgroup$ May 7, 2020 at 9:57
  • $\begingroup$ (a // MatrixForm) /. x_?NumberQ :> NumberForm[x, {2, 2}] $\endgroup$
    – Bob Hanlon
    May 7, 2020 at 13:36

1 Answer 1

7
$\begingroup$

Export suffers similar problem. The solution I found is, use *Form only on numbers:

a = {{1.3432, 34.432123}, {5.65454, 78.9883}};
Clear[numberForm]
numberForm[a_List, n_] := numberForm[#, n] & /@ a
numberForm[a_, n_] := NumberForm[a, n]
b = numberForm[a, {2, 2}]
MatrixForm@b

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.