# Data output in the specific scientific form

If I have data of

data = Table[f[x,y],{x,{1,2}},{y,{1,2}}]


which gives

{{f[1,1],f[1,2]},{f[2,1],f[2,2]}}

Then Export["file.txt",data,"Table"] gives

f[1, 1]   f[1, 2]
f[2, 1]   f[2, 2]


f[i,j] is a real number, and I would like to output it in the following form:

(sign)(number in 4 precision)(E)(sign of E)(powers of E)

For instance, if the number is 1120.14, I want it to be expressed as +1.120E+03 and if the number is -0.0315377, I want it expressed as -3.153E-02.

Is this possible? Also, when I use NumberForm, the output looks as list

{{f[1,1],f[1,2]},{f[2,1],f[2,2]}}

## 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

func[x_, y_] := Module [{precisiongoal = y,
number = x,
b = If[x >= 0, "+", "-"]
},
prova =
If[b == "-" , StringDrop[ StringDelete[ToString[number], "."], 1],
StringDelete[prova, "."]];
decimal  = StringPosition[ prova,  n_ /; n != "0" ][[1, 1]];
b <> StringTake[
StringDrop[StringInsert[prova , ".", 1 + decimal], decimal - 1],
UpTo[precisiongoal + 1]] <> "e" <>
If[b == "+", ToString[ Evaluate[decimal - 1]],
ToString[ - Evaluate[decimal - 1]]  ]

]


it should work