4
$\begingroup$

I want create file with some text (and numbers). This is my code.

str=OpenWrite["f:\\1.txt",PageWidth->Infinity, FormatType->StandardForm,NumberMarks->True];
Write[str, "a = ", 0.123456789];
Close[str]

The problem is there is rounded number (0.123457) in the output file. but I actually want file with text

a = 0.123456789

Explaining: I want parse a test suite files (*.mt) with data for check. Rows looks like

dataAcc = {{1.865987700, 1.920469000, 1.811506300, ...},...}
Get[childTestLocation<>"nameOfChildTest.mt"]

I have a numbers for dataAcc list. But when i create testfile numbers becomes rounded.

$\endgroup$
5
  • $\begingroup$ Maybe you can specify the precision of a using a = 0.123456789`9 $\endgroup$
    – Yi Wang
    Jun 6, 2014 at 10:51
  • 1
    $\begingroup$ Or if you don't want low precision from definition, but instead prefer crop the precision when output, you can use a=0.123456789; SetPrecision[a, 9]. The latter outputs 9 digits. $\endgroup$
    – Yi Wang
    Jun 6, 2014 at 10:54
  • $\begingroup$ It's solve the problem. But interesting is there another way to solve this problem $\endgroup$ Jun 6, 2014 at 11:01
  • $\begingroup$ another way: Write[ .. , "a = 0.123456789" ]. Seriously you need to explain a little better exactly what you are trying to do, $\endgroup$
    – george2079
    Jun 6, 2014 at 12:00
  • $\begingroup$ If you want to retain precision of arbitrary number, you could wrap it with FullForm: Write[str, "a = ", FullForm[a]] $\endgroup$
    – jkuczm
    Jun 6, 2014 at 16:34

1 Answer 1

1
$\begingroup$

I guess that you are after something like:

ToString[NumberForm[0.123456789, {∞, 9}]]

to explicitly specify the number of decimal places. For example, using WriteString which does the ToString bit for you:

str = OpenWrite["test.txt"];
WriteString[str, "a = ", NumberForm[0.1234, {∞, 9}], "\n"];
WriteString[str, "b = ", NumberForm[0.123456789, {∞, 9}], "\n"];
Close[str]

Which results in:

FilePrint["test.txt"]

a = 0.123400000
b = 0.123456789

But really you need to be more specific about what you want as george2079's comment has a solution with a lot less typing and using Mathematica as a text editor is not very efficient!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.