Reading in scientific notation from C++ to Mathematica
Similar to Rolf's method:
string = "f[38.67]=-2.5387862698183892298317350539374412777263289550697e-05;";
StringReplace[string, {"e+" :> "*^", "e-" :> "*^-"}];
ToExpression@%;
?? f
Global`f f[38.67] = -0.000025387862698183892298317350539374412777263289550697
Depending on your data you may want a more specific pattern, e.g.:
StringReplace[string,
{a : NumberString ~~ "e" ~~ b : NumberString :> a <> "*^" <> b}]
Import
usually automatically converts the e format to powers of. You can use ImportString
with the "Table"
or "List"
type:
ImportString["-2.5387862698183892298317350539374412777263289550697e-05", "Table"]
{{-0.000025387862698183892298317350539374412777263289550697}}
or
ImportString["1.002e-26", "Table"]
{{1.002*10^-26}}
For those wishing to transform a full data file with numbers in scientific notation that is output from Fortran (or C, et al.) as "1.6E-19"
and "3.0E+08"
and such, it is incredibly faster to make changes in the Unix sed
command line editor than doing a search and replace in the .m file within Mathematica or any word processor. At the Unix prompt $
one types
$ cat FilewE.m | sed 's/E-/*^-/g' | sed 's/E+/*^+/g' > FilewoE.m
For those new to Unix, the cat
command "concatenates" a single file (named FilewE.m
in this example), which normally would flow the contents onto the terminal screen. Instead the flow is piped |
into sed
and, to avoid killing off any E
s that appear in words, E-
is replaced with *^-
globally g
. The output of that is piped into sed
again to catch the positive exponents and the output of that is directed >
to the output file named FilewoE.m
in this example.