MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Outputting to file with fixed decimal digits

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75271] Re: Outputting to file with fixed decimal digits
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 23 Apr 2007 05:39:05 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f0ekbt$pnn$1@smc.vnet.net>

actuary at mchsi.com wrote:
> Hello:
> 
> I'm trying to create a file that becomes input to another software
> package.  I need to output real numbers with a fixed number of decimal
> digits with spaces separating the numbers that I write to the file.
> 
> I've  searched this group for assistance on my question and couldn't
> find anything dealing with outputting to a file.
> 
> Help would be greatly appreciated.
> 
> Larry
> 

Depending on how you wish (or must) handle the creation of the file, you 
could use *Export* and *NumberForm*. The following should help you to 
start. The symbol expr holds some toy data as an array of numbers 
(In[1]), then is is converted in the form of a list of strings (In[2]) 
that is going to be exported in a text file (In[3]). The file is read 
from within Mathematica to check its format (In[4]).

In[1]:=
expr = {{1.234567987654*^6, -34.3892839, 898890.7348327,
     -8.748328494823489*^8}, {124567.98764, -8934.3892839, 89890.7348327,
     -8.78907284948235*^9}}

Out[1]=
             6                                8
{{1.23457 10 , -34.3893, 898891., -8.74833 10 },

                                           9
   {124568., -8934.39, 89890.7, -8.78907 10 }}

In[2]:=
data = (StringReplace[#1, Characters["{,}"] -> ""] & ) /@
    (ToString[NumberForm[#1, {10, 2}, NumberPadding -> {"", "0"},
        ExponentFunction -> (Null & )]] & ) /@ expr

Out[2]=
{1234567.99 -34.39 898890.73 -874832849.50,

   124567.99 -8934.39 89890.73 -8789072849.00}

In[3]:=
Export["C:\\myfile.txt", data, "Lines"]

Out[3]=
C:\myfile.txt

In[4]:=
!! C:\\myfile.txt

 From In[4]:=
           6                             8
1.23457 10  - 34.39 898891. - 8.74833 10
                                       9
124568. - 8934.39 89890.7 - 8.78907 10

Regards,
Jean-Marc



  • Prev by Date: Exporting images without white borders
  • Next by Date: Re: question
  • Previous by thread: Re: Outputting to file with fixed decimal digits
  • Next by thread: Re: Outputting to file with fixed decimal digits