0

My first question is how would I export my data in a fixed-width format using Mathematica? My second question is how do I preserve the right most 0's.

For instance, I like to save {{1.12300, 11.12, 111.123},{2.1, 22.123, 222}} into a text file as

1.12300  11.12   111.123
2.10     22.123  222.00

In details, if a number has a mantissa with less than 2 digits, it is matched to 2 via zero padding while if it has more than 2 digits in its mantissa, it would preserve it as it is. It is important for me to distinguish 1.12300 from 1.123. If I use PaddingForm, Mathematica would literally save it as PaddedForm[1.123, {4, 5}] in a text file.

Miladiouss
  • 4,270
  • 1
  • 27
  • 34
  • it is important to understand that a number such as "11.12" , once keyed in or returned as a result is a machine precision binary represented floating point number, and is no longer "exactly" 1112/100. You should go to mathematica.stackexchange.com for further info, but do a search there are literally dozens of similar questions. – agentp May 05 '17 at 14:06

3 Answers3

1

In order to preserve the trailing zeros on numbers such as 1.12300 the data has to be received as a string. Then it can be processed like so.

data = "{{1.12300, 11.12, 111.123}, {2.1, 22.123, 222}}";

(* remove any whitespace *)
d1 = StringReplace[data, " " -> ""];

(* split the lists *)
d2 = StringSplit[StringTake[d1, {3, -3}], "},{"];

(* split the numbers *)
d3 = StringSplit[d2, ","];

(* magnitude of number except zero *)
mag[n_] := Floor[Log[10, Abs[n]]] + 1

(* format accordingly *)
d4 = Map[With[{x = ToExpression[#]},
     Which[x == 0, If[StringLength[#] > 4, #, "0.00"],
      FractionalPart[100 x] == 0,
      ToString@NumberForm[x, {mag[x] + 2, 2},
         ExponentFunction -> (Null &)],
      True, #]] &, d3, {2}];

(* pad output *)
len = Max[StringLength /@ Flatten[d4]] + 2;
d5 = Map[StringPadRight[#, len] &, d4, {2}];
d6 = StringJoin /@ d5;
Export["output.txt", d6];
Import["output.txt"]
1.12300  11.12    111.123
2.10     22.123   222.00
Chris Degnen
  • 8,443
  • 2
  • 23
  • 40
  • Yes, it does generate that example, but the issue is the fixed-width format must use spaces rather than tabs. Unfortunately, this is a format common in astronomy data. Also, the code would not work properly if I change the first element to 1.12300000. – Miladiouss May 05 '17 at 19:19
  • @mpourrah No problem to implement fixed-width format if you write the specification. The padding is interesting - a problem that crops up often. How should the columns be formatted if the first number is 1.12300000? – Chris Degnen May 05 '17 at 19:33
  • I guess I can find out which element of each column has the most number of characters. – Miladiouss May 05 '17 at 20:49
  • @mpourrah I have added a method along that line. – Chris Degnen May 06 '17 at 10:13
1

Use

data = {{1.12300``6, 11.12``3,  111.123``4}, 
        {2.1``2,     22.123``4, 222``2}};

tbl1 = ToString[TableForm[data]];
tbl2 = StringReplace[tbl, "\n\n" -> "\n"]

to get

1.12300   11.12    111.123
2.1       22.123   222.0

If you don't want to enter your data as a set of strings, you need to specify the accuracy of your data using ``. See Numerical Precision tutorial by Mathematica.

Miladiouss
  • 4,270
  • 1
  • 27
  • 34
0

Something like this?

listt = {{1.12300, 11.12, 111.123}, {2.1, 22.123, 222}}

Export["C:/tcdata/list.txt", Flatten /@ listt, "Table"]
zhk
  • 331
  • 2
  • 16