Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg75283] Re: Outputting to file with fixed decimal digits
  • From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 23 Apr 2007 05:45:21 -0400 (EDT)
  • References: <042220071303.18591.462B5D15000628FB0000489F219791332903010CD2079C080C03BF979D0E9A9B0C0E@mchsi.com>

On 4/22/07, actuary at mchsi.com <actuary at mchsi.com> wrote:
>
>
>
>
> Jean-Marc:
>
>
>
> Thank you.  I think I'm half the way to solving my problem.
>
>
>
> In my posting I  failed to provide a piece of information that I didn't
> think was important until I implemented your suggestion.
>
>
>
> The data that I work with is record that I Import into Mathematica.  The
> record has 14 data points.  I use Mathematica to select specific records.
> At the end of the day I out only 2 of the 14 data points.
>
>
>
> The first data point is the Julian Date  and the second data point is the
> magnitude of a star.
>
>
>
> The value for the JUlian Date is of the form xxxxxxx.x or xxxxxxx.xxxx.  The
> file that I want to create and use by another software program needs to have
> the Julian Date of the form xxxxxxx.xxxx
>
>
>
> Prior to implementing your suggestion my out from Mathematica looks like
> this
>
>
>
> 2.4162256e+6   10.
> 2.4162266e+6   10.3
> 2.4162326e+6   9.9
> 2.4162336e+6   9.9
> 2.4164746688e+6   9.
> 2.4164746743e+6   8.8
> 2.4164894813e+6   9.199999999999999
> 2.4165204917e+6   10.1
>
>
>
> What I need is
>
>
>
> 2416225.6000   10.0
> 2416226.6000   10.3
> 2416232.6000   9.90
> 2416233.6000   9.90
> 2416474.6688   9.00
> 2416474.6743   8.80
> 2416489.4813   9.20
> 2416520.4917   10.1
>
>
>
> While your suggested provides the alignment that I need, I don't have the
> decimal portion of the Julian Date.
>
>
>
> My computer programming background is limited.  Fortran training 30 years
> ago.
>
>
>
> Thanks for any assistance that you may provide.
>
>
>
> Larry Gorski

Hi Larry,

Assuming I have correctly interpreted your date format, the following
expression should fit your need.

In[1]:=
expr = {{2.4162256*10^6, 10.}, {2.4162266*10^6, 10.3},
    {2.4162326*10^6, 9.9}, {2.4162336*10^6, 9.9},
    {2.4164746688*10^6, 9.}, {2.4164746743*10^6, 8.8},
    {2.4164894813*10^6, 9.2}, {2.4165204917*10^6,
     10.1}};

data = (StringReplace[#1, Characters["{,}"] -> ""] & ) /@
   (ToString[{NumberForm[##1[[1]], {12, 4},
        NumberPadding -> {"", "0"},
        ExponentFunction -> (Null & )],
       If[##1[[2]] < 10, NumberForm[##1[[2]], {3, 2},
         NumberPadding -> {"", "0"}],
        NumberForm[##1[[2]], {3, 1}, NumberPadding ->
          {"", "0"}]]}] & ) /@ expr;

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

!!C:\\myfile.txt

Out[3]=
C:\myfile.txt

>From In[1]:=
2416225.6000 10.0
2416226.6000 10.3
2416232.6000 9.90
2416233.6000 9.90
2416474.6688 9.00
2416474.6743 8.80
2416489.4813 9.20
2416520.4917 10.1

Regards,
Jean-Marc

> -------------- Original message from Jean-Marc Gulliet
> <jeanmarc.gulliet at gmail.com>: --------------
>
>
> > 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 fo rm 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 -87 89072849.00}
> >
> > In[3]:=
> > Export["C:\\myfile.txt", data, "Line s"]
> >
> > 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: Re: Piecewise and Integral
  • Next by Date: Re: Barchart Questions
  • Previous by thread: Re: Outputting to file with fixed decimal digits
  • Next by thread: Re: Outputting to file with fixed decimal digits