I want to produce a number-form like 1.23456x10^-2 => "+12.35e-3". I.e. something similar to EngineeringForm, but as a flat string in Fortran-Notation ("e" instead of "x10^") and with a mandatory sign.
Clear[x2e31];
x2e31[x_, digs_Integer : 3, signs_ : True] :=
TextString[If[x != 0,
NumberForm[x, digs, NumberFormat -> (Row[{#1, "e", #3}] &),
ExponentFunction -> (3 Quotient[#, 3] &),
NumberSigns -> If[signs, {"-", "+"}, {"-", ""}]]
, If[signs, "+0", "0"]]];
x = 1.23456*10^-2; {x, EngineeringForm[x, 4], x2e31[x, 4]}
==> {0.0123456, 12.35x10^-3, +12.35*10^-3}
I cannot succeed. Either the NumberForm-option NumberFormat is unevaluated as above or the mantissa does not have digs significant digits as below:
Clear[x2e32];
x2e32[x_, digs_Integer : 3, signs_ : True] :=
TextString[If[x != 0,
NumberForm[N[x, digs], NumberFormat -> (Row[{#1, "e", #3}] &),
ExponentFunction -> (3 Quotient[#, 3] &),
NumberSigns -> If[signs, {"-", "+"}, {"-", ""}]]
, If[signs, "+0", "0"]]];
x = 1.23456*10^-2; {x, EngineeringForm[x, 4], x2e32[x, 4]}
==> {0.0123456, 12.35x10^-3, +12.3456e-3}
The only difference between the two versions ist NumberForm[N[x, digs], ...] instead of NumberForm[x, digs, ...].
Can anybody explain and resolve that strange behavior of NumberForm?