Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Maybe you can specify the precision of a using a = 0.123456789`9 –  Yi Wang Jun 6 at 10:51
1  
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. –  Yi Wang Jun 6 at 10:54
    
It's solve the problem. But interesting is there another way to solve this problem –  molekyla777 Jun 6 at 11:01
    
another way: Write[ .. , "a = 0.123456789" ]. Seriously you need to explain a little better exactly what you are trying to do, –  george2079 Jun 6 at 12:00
    
If you want to retain precision of arbitrary number, you could wrap it with FullForm: Write[str, "a = ", FullForm[a]] –  jkuczm Jun 6 at 16:34

1 Answer 1

up vote 1 down vote accepted

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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