Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

export to file with a specific precision

849 views
Skip to first unread message

miguelwon

unread,
Aug 3, 2010, 6:37:41 AM8/3/10
to
Hello.

I need to export to a file several numbers with a specific number of
digits on the right of the decimal. I'm trying this with the function
NumberForm[] and Export[] but the problem is that export function is
not exporting only the number but the full inputform. For example, if
I do this:

Export["teste.dat", NumberForm[1.23456, {3, 4}]]

it will write in the file this:

NumberForm[1.23456, {3, 4}]

It happens the same with:

NumberForm[1.23456, {3, 4}]>>"teste.dat".

Another problem is that in the end I need the data in fortran form,
which mean I can't pass to a string before exporting because for the
cases of raw data that are in scientific format the final result will
be a string with "10^x" which is not fortran format.

Thanks,

Miguel


Albert Retey

unread,
Aug 4, 2010, 5:48:57 AM8/4/10
to
Hi,

Actually I think that converting to String is what you want in this
case, note that you can use ToString together with NumberForm or
FortranForm:

ExportString[ToString@NumberForm[1.23456, {3, 4}], "Table"]

ExportString[ToString@FortranForm[1.23456*10^-8], "Table"]

should both work. If you need 4 digits and the fortran like e notation
you could do something like this:

ExportString[ToString@NumberForm[1.23456*10^-8, {3, 4},
NumberPadding -> {"", "0"},
NumberFormat -> (If[#3 === "", #1, Row[{#1, "e", #3}]] &)
], "Table"]

Note: Export with a filename exports the result of ExportString to a
filename, so you can use ExportString as a convenient way to see what
Export would do.

hth,

albert

David Park

unread,
Aug 4, 2010, 5:49:40 AM8/4/10
to
Have you tried the Round function?

Round[N[1/3], .01]
% // InputForm

0.33

0.33


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/

From: miguelwon [mailto:migu...@gmail.com]

Hello.

I need to export to a file several numbers with a specific number of
digits on the right of the decimal. I'm trying this with the function
NumberForm[] and Export[] but the problem is that export function is
not exporting only the number but the full inputform. For example, if
I do this:

Export["teste.dat", NumberForm[1.23456, {3, 4}]]

it will write in the file this:

NumberForm[1.23456, {3, 4}]

It happens the same with:

NumberForm[1.23456, {3, 4}]>>"teste.dat".

Another problem is that in the end I need the data in fortran form,
which mean I can't pass to a string before exporting because for the
cases of raw data that are in scientific format the final result will
be a string with "10^x" which is not fortran format.

Thanks,

Miguel


0 new messages