3
$\begingroup$

I have a report that I currently export to Excel in order to format it nicely. I'd like to be able to produce a reasonably well formatted report directly in Mathematica. It looks like I can use Grid in order to set up the structure of the report. However I'd like to also format the numbers. I see that I can use NumberForm to handle some of the formatting, but I can't seem to then get the output from NumberForm to work inside Grid. Probably because NumberForm "prints" the results. Additionally I have some columns that appear in scientific notation if the number is large enough. I would like that column to have all in scientific notation or none in scientific notation. I tried using ExponentFunction in NumberForm, but without begin able to embed NumberForm results in Grid it didn't work. I was hoping that there might be some global setting for this threshold.

In addition to the above, I'd appreciate any suggestions about formatting a 2-dimensional table in Mathematica without having to go to Excel.

EDIT - sample:

I haven't gotten as far as headings. I'd want to align the columns. Colums 2 & 4 should probably stay in scientific notation for all numbers. columns 3 & 5 are probably best as integers - I can round them. A sample output, without formatting or headings is as follows.

Grid[Values[joinPMLs[group1CA, group2CA]] /. Missing["Unmatched"] -> "NA"]

enter image description here

$\endgroup$
2
  • $\begingroup$ Please provide minimal sample data (array) and column/row headings that represent the range of issues to be addressed. $\endgroup$
    – Bob Hanlon
    Commented Apr 12, 2017 at 19:50
  • $\begingroup$ you need to appy NumberForm to individual entries, ie Grid[Map[NumberForm[#, 2] &, matrix, {2}]] , not Grid[NumberForm[matrix, 2]]] See also TableForm $\endgroup$
    – george2079
    Commented Apr 12, 2017 at 20:30

2 Answers 2

2
$\begingroup$
TableForm[
 {#[[1]], EngineeringForm[#[[2]], {3, 1}]} & /@ 
  Table[{RandomReal[], RandomReal[10^5]}, {10}],
  TableHeadings -> {None, {"Normal", "Engineering Form"}}
 ]  
$\endgroup$
2
$\begingroup$

If you have an association, you can do it right away, if you have a list of lists, you need to convert it into an association and then perform formatting on individual columns, similar to what you would do in Excel, after which you can use Grid or any other layout function:

data = {{1, 200000, 3, "string", Sin[z]/2}, 
        {100, 200, 1.5, 300, Sum[1/Cos[x], {x, a, b}]}};

adata = Inner[Rule,Table["C" <> ToString@i, {i, Length[data[[1]]]}], Transpose[data], Association];

Then you can do something like this:

(Query[All, {
            "C2" -> N /* (ScientificForm[#, 5] &),
            "C3" -> Round, 
            "C4" -> (If[IntegerQ@#, IntegerName[#], #] &),
            "C5" -> TraditionalForm
            }] @ adata) // Normal // Values // Grid

enter image description here

$\endgroup$
1
  • $\begingroup$ My data originally was in a list of associations, but I went to values for my output to Excel. For now, David's solution works well on my data. I'll revisit your solution when I have more time to go back over my project and keep the data as a list of associations. There may be other advantages I can gain by keeping it that way. $\endgroup$ Commented Apr 12, 2017 at 21:24

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.