Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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];

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, ...},...}

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 '14 at 10:51
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 '14 at 10:54
It's solve the problem. But interesting is there another way to solve this problem – molekyla777 Jun 6 '14 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 '14 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 '14 at 16:34
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"];

Which results in:


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


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.