I am trying to copy some numerical values of parameters from a plain text file into Mathematica, then arranging them in a list and exporting them to another text file, with .ini extension.
My problem is that I want the exact same precision for the output file, as the one I have in the input, but Mathematica is truncating the output, no matter what I do. I am aware of this question Annoying display truncation of numerical results, but the methods there are not working for me.
This is what I am trying:
a = 70.3029753444001
70.303
bparam = 0.225980870923660
0.225981
paramlist = {aparam, bparam}
This is my exporting function:
exportText[parlist_,
name_] := (outtext = {"a = " <> ToString[parlist[[1]]],
"b = " <> ToString[parlist[[2]]]};
Export["./" <> ToString[name] <> ".ini", outtext, "Text"])
And now I export:
exportText[paramlist, "param1"]
The output in my param1.ini file is of course:
a = 70.303
b = 0.225981
I cannot use:
SetOptions[InputNotebook[], PrintPrecision-> 10]
Because I want for each number I copy into Mathematica, the same precision in the output, and all parameters have different precisions.
Also using NumberForm as was suggested to me in the other post, ruins my output:
exportText[NumberForm[#, {10, 7}] &@paramlist, "param1"]
The file looks like:
a = {70.303, 0.225981}
b = {10, 7}
And anyway this is not dynamical, for any number I copy into Mathematica.
I tried to create a dynamical function:
setPrecisionOfNumber =
SetPrecision[#, Length[RealDigits[#][[1]]] - RealDigits[#][[2]]] &;
But then this adds extra zeros at the end, for example:
setPrecisionOfNumber@bparam
0.2259808709236600
setPrecisionOfNumber@0.2541
0.2541000000000000
So what is the solution you propose?
I have of course tried things like N[aparam,10]
but it doesn't work.
And anyway I need it to be dynamical.
I would be grateful for ideas on how to solve this.
EDIT Using @billisphere solution, I changed my exportText function definition to:
exportText[parlist_,
name_] := (outtext = {"a = " <>
ToString[parlist[[1]], InputForm, NumberMarks -> False],
"b = " <> ToString[parlist[[2]], InputForm]};
stringfile = ExportString[outtext, "Text"];
Export["./" <> ToString[name] <> ".ini", outtext, "Text"])
I still want to understand certain things of number representation. As you can see, if I don't use NumberMakrs->False
, I get for a number like:
bparam = 0.225980870923660011
The following prints in my file:
FilePrint["./param1.ini"]
a = 70.3029753444001
b = 0.225980870923660011`17.35407167806817
Using NumberMakrs->False
also for the bparam, I get:
FilePrint["./param1.ini"]
a = 70.3029753444001
b = 0.225980870923660011
I still want to understand why there are still truncations, if I have a number like:
bparam = 0.22598087092366001
The printed file is then:
FilePrint["./param1.ini"]
a = 70.3029753444001
b = 0.22598087092366
So, the 001 at the end of bparam was truncated. If I add another 1 at the end of bparam, I don't get any truncation.
I know these numbers are quite large with many decimal digits, and in the praxis I don't need this. But I want to be sure that my output is the same as my input in all cases.