Re: How to write formatted number into a file

*To*: mathgroup at smc.vnet.net*Subject*: [mg66611] Re: [mg66607] How to write formatted number into a file*From*: Christopher Purcell <christopherpurcell at mac.com>*Date*: Wed, 24 May 2006 03:01:45 -0400 (EDT)*References*: <200605222215.SAA09809@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Sorry for the long winded post. Like you, I am not happy with the built in "Forms" to build Fortran compatible numbers for writing to text files, so here is a homemade E-format which may be a starting point for your problem. It likely does more than you need, but you get what you pay for. Eformat::usage="Eformat[x,{total,ndigits}] returns Fortran E Format representation of the number x. Numbers whose exponents are greater than 99 are treated as errors. Note that zero is detected by comparison with $MachineEpsilon and printed as 0.0000.. without the exponent."; Eformat::notallowed="This function will not accept `1` as input, it only works on Integer or Real numbers"; Eformat::toomany="Too many digits to right of decimal Point requested."; Eformat[0,{tot_Integer,ndigits_Integer}]:= Eformat[0.,{tot,ndigits}]; Eformat[x_Rational,{tot_Integer,ndigits_Integer}]:= Eformat[N[x],{tot,ndigits}]; Eformat[x_Integer,{tot_Integer,ndigits_Integer}]:= Eformat[N[x],{tot,ndigits}]; Eformat[x_Real,{tot_Integer,ndigits_Integer}]:= Module[{exp,man,junk},( If[(tot-ndigits)>=7,( If[Abs[x]<$MachineEpsilon,( SequenceForm[" ",PaddedForm[0.,{tot,ndigits+4},NumberPadding-> {"","0"}]]), (junk=MantissaExponent[x,10]; {man,exp}={PaddedForm[junk[[1]]*10.,{tot,ndigits},NumberPadding-> {"","0"}],junk[[2]]-1}; If[Abs[exp]<=99,( If[Abs[exp]<10,( SequenceForm[If[Sign[x]==-1,""," "],man,"E", If[exp>=0,"+","-"], PaddedForm[Abs[exp],1, NumberPadding->{"0",""}]]),( SequenceForm[If[Sign[x]==-1,""," "],man,"E", If[exp>=0,"+","-"], PaddedForm[Abs[exp],1, NumberPadding->{"",""}]])]), SequenceForm[If[Sign[x]==-1,""," "],man,"E","***"]] )]),Message[Eformat::toomany]])]; Eformat[x_,{tot_Integer,ndigits_Integer}]:= Eformat[N[x],{tot,ndigits}]; (********************************************************************) (* Here is a typical usage: *) Eformat[-Pi*10^21,{10,3}] -3.142E+21 (* To use this to write a file you could do this: *) data=Map[Eformat[#,{12,5}]&,Table[n Pi,{n,1,5}]]; (* invent some data *) out=OpenWrite["junk.txt"]; (* write it to a text file *) Scan[WriteString[out,#,"\n"]&,data]; Close[out]; (* now look at the file *) !!junk.txt 3.14159E+00 6.28319E+00 9.42478E+00 1.25664E+01 1.57080E+01 Hope this is what you were seeking. christopherpurcell at mac.com On May 22, 2006, at 7:15 PM, phd related wrote: > Hi, I am try to write a formated number into a file. For example, > Writing > 6.661999999999944*10^-6 as 6.66200E-06 . > If I use ScientificForm and can see the format 6.66200E-06 in > mathematica, > however when i am trying to write it into a file like the what i > see in > mathematica, > the I see ScientificForm instead of the result i desired. > How to tackle this problem? >

**References**:**How to write formatted number into a file***From:*"phd related" <phdrelated@gmail.com>