2
$\begingroup$

Given:

mat1 = {{"", "", X1, X2, X3, X4, X5, X6}, {reg1, count1, 
12345.6786546, 0.9714322786600009, 0.39860039818934734, 
0.426662162330312, 1.792374088961013, 0.4140144436761481}, {reg2, 
count2, 12345.16786546, 0.9889249587288553, 1.780535580027618, 
1.60781000579517, 0.577725163636736, 0.1175474621597238}, {reg3, 
count3, 12345.0786546, 1.824433941221109, 0.3126457769687216, 
0.357252998031583, 0.961893471647981, 1.510169721094909}, {reg4, 
count4, 12345.26786546, 0.576753111943114, 0.1973429756544189, 
1.52173753847146, 1.901889363592111, 0.61061606981908}, {reg5, 
count5, 12345.976786546, 1.27977000137376, 1.613078010044809, 
1.992528170481412, 1.28320604946023, 1.974675522449778}};

NumberForm[TableForm[mat1], {7, 4}]

Generates:

enter image description here

Touring the past questions, I could not really find an answer to my question. (@BobHanlon shows a way to use NumberForm for individual variables, though. It does not work in my case.)

Is there any way to use NumberForm for individual columns, for example, X1, and delete all the digits in column X1?

Is there any way to use NumberForm after creating a Dataset?

EDIT The rule I like to apply is: if the number concerned in TableForm is greater than 1, then drop the digits if any; and if the number is less than 1, keep, say, 3 or 4 digits.

$\endgroup$
3
$\begingroup$
TableForm[Map[If[NumericQ[#], If[# > 1, Floor @ #, Round[#, .001]], #] &, mat1, {-1}]]

enter image description here

$\endgroup$
3
$\begingroup$
mat1 = {{"", "", X1, X2, X3, X4, X5, X6}, {reg1, count1, 
    12345.6786546, 0.9714322786600009, 0.39860039818934734, 
    0.426662162330312, 1.792374088961013, 0.4140144436761481}, {reg2, 
    count2, 12345.16786546, 0.9889249587288553, 1.780535580027618, 
    1.60781000579517, 0.577725163636736, 0.1175474621597238}, {reg3, 
    count3, 12345.0786546, 1.824433941221109, 0.3126457769687216, 
    0.357252998031583, 0.961893471647981, 1.510169721094909}, {reg4, 
    count4, 12345.26786546, 0.576753111943114, 0.1973429756544189, 
    1.52173753847146, 1.901889363592111, 0.61061606981908}, {reg5, 
    count5, 12345.976786546, 1.27977000137376, 1.613078010044809, 
    1.992528170481412, 1.28320604946023, 1.974675522449778}};

nbrForm[x_] := Module[{log = Floor@Log10@x},
  NumberForm[x, {5, Min[4, 4 - log]},
   NumberPadding -> {" ", "0"}]]

TableForm[mat1 /. x_Real :> nbrForm[x],
 TableAlignments -> Center]

enter image description here

$\endgroup$
2
  • $\begingroup$ I tried your code using my actual table, and it does not work as expected. The reason is that, in my question, for simplicity, I used X1 in the format given. In the actual table, I have something like 12.02, 367.344, 6754.5676, 3.2, 16549.34 for X1. I guess my question was not formulated correctly. The rule I like to apply is: if the number concerned in TableForm is greater than 1, then drop the digits if any; and if the number is less than 1, keep, say, 3 or 4 digits. I will edit the question in a moment. $\endgroup$ – Tugrul Temel Mar 27 at 23:22
  • 1
    $\begingroup$ The revised requested format would seem to ruin the formatting for columns x2 thru x6 $\endgroup$ – Bob Hanlon Mar 27 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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