爲了保存上的數字,例如1.12300
數據有尾隨零將被接收爲一個字符串。然後它可以像這樣處理。
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,
[email protected][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
重要的是要理解一個數字,例如「11.12」,一旦鍵入或返回結果是一個機器精度二進制代表浮點數,不再是「完全」1112/100。你應該去mathematica.stackexchange.com進一步的信息,但做一個搜索有幾十個類似的問題。 – agentp