How to use NumberForm for producing a Fortran-like EngineeringForm?

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?

Hi Werner,

Looks like the problem is caused by TextString, NumberForm appears to be working fine. After removing TextString

{x, EngineeringForm[x, 4], x2e31[x, 4]}
(* {0.0123456, 12.35*10^(-3), +12.35e-3} *)

Yes, this works. Pretty strange, since TextString is evaluated after NumberForm. The reason might be that NumberForm is just an output-wrapper which does not affect evaluation.
But why, in x2e31, digs is respected, but NumberFormat is not?
And why does TextString[EngineeringForm[x,4] work?

Anyway, omitting TextString is a bypass. Thank you!
But until now I don’t know how to use x2e31 then as a string concatenated with other strings.

Werner,

Look at this thread. I show several ways to do the formatting. My last post in the thread is most relevant. Thread link here . For the most flexibility in getting exactly the format you want, I would use my SPrintf[] function. The Paddedform solution is not bad as well.

Would either of these work for you?

Regards,

Neil

I found a solution now which does everything I need and gives a string. It seems to be similar to Neils suggestions in that it works with StringReplace:

Clear[x2e3];
x2e3[x_, digs_Integer : 3, signs_ : True] :=
  StringReplace[TextString[If[x != 0,
     EngineeringForm[x, digs, 
      NumberSigns -> If[signs, {"-", "+"}, {"-", ""}]]
     , If[signs, "+0", "0"]
     ]], {".*10^" -> "e", "*10^" -> "e"}];

Note that I ommit a decimal point at the end of the mantissa. I.e x=0.123456 = 123.*10^-3 => x2e[x] = “+123e-3”.

If[True, Block[{x, digs = 4},
  Table[{i,
     x = If[NumericQ[i], If[OddQ[i], -1, 1]*1.23456*10^i, 0],
     N[x, digs],
     TextString[EngineeringForm[x, digs]] // InputForm,
     x2e3[x, digs, True] // InputForm,
     x2e3[x] // InputForm
     }, {i, Append[Range[-6, 6], "-"]}
    ] // TableForm
  ]]

-6	1.23456*10^-6      1.23456*10^-6	"1.235*10^-6"	"+1.235e-6"	"+1.23e-6"
-5	-0.0000123456     -0.0000123456	    "-12.35*10^-6"	"-12.35e-6"	"-12.3e-6"
-4	0.000123456        0.000123456      "123.5*10^-6"	"+123.5e-6"	"+123e-6"
-3	-0.00123456 	  -0.00123456	    "-1.235*10^-3"	"-1.235e-3"	"-1.23e-3"
-2	0.0123456          0.0123456        "12.35*10^-3"	"+12.35e-3"	"+12.3e-3"
-1	-0.123456         -0.123456         "-123.5*10^-3"	"-123.5e-3"	"-123e-3"
0	1.23456            1.23456          "1.235"        	"+1.235"	"+1.23"
1	-12.3456         -12.3456	        "-12.35"    	"-12.35"	"-12.3"
2	123.456          123.456        	"123.5"        	"+123.5"	"+123."
3	-1234.56	    -1234.56        	"-1.235*10^3"	"-1.235e3"	"-1.23e3"
4	12345.6         12345.6            	"12.35*10^3"	"+12.35e3"	"+12.3e3"
5	-123456.      -123456.            	"-123.5*10^3"	"-123.5e3"	"-123e3"
6	1.23456*10^6     1.23456*10^6       "1.235*10^6"	"+1.235e6"	"+1.23e6"
-	0                	0            	"0"            	"+0"    	"+0"