Rounding
I'm going to first assume that you really want the precision you claimed, 4 digits, as opposed to a hard truncation like your example, which is a nice out of box application of NumberForm
:
format[num_] :=
NumberForm[num, {4, 3},
ExponentFunction -> (If[-1 < # < 1, Null, #] &),
NumberFormat -> (SequenceForm[#1, "E", #3] &),
NumberSigns -> {"-", "+"}]
applyFormatting[expr_] := expr /. a_ :> format[a] /; NumberQ[a]
tab1 = {{5.6^10, 7.8^20}, {1120.14, -0.0315377}} //
applyFormatting;
Yields:
{{+3.033E7, +6.949E17}, {+1.120E3, -3.154E-2}}
Notice the formatting is applied to the numbers themselves not to the table. So Export
and TableForm
should work as you like.
e.g
TableForm[tab1]
gives:
+3.033E7 +6.949E17
+1.120E3 -3.154E-2
and you can use:
Export["~/table1.tab", tab1, "Table"]
Truncating
Now if you really insist on a hard truncate, like you did in your example, this is literally throwing away information:
-0.0315377
$\mapsto$-3.153E-02
I guess you could try starting with something like:
truncationFormat[num_] :=
( Sign[num]*(Abs[(num *10^-(-3 + Floor@Log[10, Abs[num]]))] //
Floor)*10.^(-3 + Floor@Log[10, Abs[num]]) // format)/;NumberQ[num]
applyTruncation[expr_] :=
expr /. a_ :> truncationFormat[a] /; NumberQ[a]
{{5.6^10, 7.8^20}, {1120.14, -0.0315377}} // applyTruncation //TableForm
yielding
+3.033E7 +6.948E17
+1.120E3 -3.153E-2