3
$\begingroup$

I am using ReadList to read an input file as Table format, and exporting the results. The input.m file contains expression (say exp). In the file I have the numerical values for the variables.

Block[{}, Export["output.m", ReadList["input.m"], "Table"] ]

While it is printing small numbers as decimal (e.g. 771640.5749154771), for bigger numbers it changes to a different format, like 2.8702708449431164e6.

Is it possible to keep the decimal format for those large numbers as well?

There are some solutions using DecimalForm, NumberForm etc. However I do not know how to implement those in this particular piece of code. Should I enclose the exp as NumberForm[exp, 15]? Or is there any alternate solution like globally setting $NumberForm?

$\endgroup$

1 Answer 1

7
$\begingroup$

The "Table" export format uses CForm to format content. So, one possibility is to block CForm to something that formats the way you want:

Block[{CForm = OutputForm @* (DecimalForm[#,15]&)},
    ExportString[
        {771640.5749154771, 2.8702708449431164*^6},
        "Table"
    ]
]
"771640.574915477
 2870270.84494312"

For Mathematica versions before 11.2, you can use NumberForm with an ExponentFunction option :

Block[{CForm = OutputForm @* (NumberForm[#, 15, ExponentFunction->(Null&)]&)},
    ExportString[
        {771640.5749154771, 2.8702708449431164*^6},
        "Table"
    ]
]
"771640.574915477
 2870270.84494312"
$\endgroup$
3
  • $\begingroup$ Thanks. Somehow on my mathematica (Mathematica 11.0.0 for Mac OS X x86 (64-bit)), DecimalForm does not give your output but gives DecimalForm[2.87027 10^6 , 15]. But changing to AccountingForm gives me your output. $\endgroup$
    – BabaYaga
    Commented Mar 16, 2019 at 5:40
  • $\begingroup$ But then problem with the AccountingForm is that it does not recognise negative numbers. $\endgroup$
    – BabaYaga
    Commented Mar 16, 2019 at 5:50
  • $\begingroup$ @Boogeyman See update $\endgroup$
    – Carl Woll
    Commented Mar 16, 2019 at 19:12

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.