This was based on one of the answers from here
ClearAll[FortranReal] ;
SetAttributes[FortranReal, Listable] ;
FortranReal[
number_Real,
precision_Integer: Ceiling[$MachinePrecision] + 1 ,
kind_String: ""
] := Block[
{mantissa, exponent, sign},
{mantissa,exponent} = If[SameQ[number,N[0,Precision[number]]],List[N[0,Precision[number]],1],MantissaExponent[number]] ;
mantissa = N[Times[10,mantissa],precision] ;
mantissa = NumberForm[mantissa,Rule[DefaultPrintPrecision,precision]] ;
exponent = Subtract[exponent,1] ;
sign = Sign[exponent] ;
sign = Which[GreaterEqual[sign,0],"+",Less[sign,0],"-"] ;
exponent = Abs[exponent] ;
mantissa = ToString[mantissa] ;
mantissa = StringTrim[mantissa,"0"..] ;
mantissa = If[SameQ[mantissa,"."],"0.",mantissa] ;
If[UnsameQ[exponent,0],StringTemplate["`1`E`2``3``4`"][mantissa,sign,exponent,kind],StringTemplate["`1``2`"][mantissa, kind]]
] ;
Examples:
FortranReal[100.0*Pi]
FortranReal[{0.1*N[E], N[E], 10*N[E]}]
FortranReal[{0.1*N[E], N[E], 10*N[E]}, 3]
FortranReal[{0.1*N[E], N[E], 10*N[E]},Ceiling[$MachinePrecision] + 1 , "_REAL64" ]
(* "3.141592653589793E+2" *)
(* {"2.7182818284590455E-1","2.7182818284590446","2.7182818284590446E+1"} *)
(* {"2.72E-1","2.72","2.72E+1"} *)
(* {"2.7182818284590455E-1_REAL64","2.7182818284590446_REAL64","2.7182818284590446E+1_REAL64"} *)
FortranFormfunction. Didn't check to see what happens if you export aFortranFormexpression directly, but if theFortranFormhead comes through, then you would just useToStringto clean that up. Or maybe someExportoption. – lericr Jun 22 '23 at 22:48