Re: a problem about mathematica output
- To: mathgroup at smc.vnet.net
- Subject: [mg82441] Re: a problem about mathematica output
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 20 Oct 2007 05:54:20 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <ff9rli$4nj$1@smc.vnet.net>
qwer wrote: > I have a puzzle when using mathematica, I want to out put my resuts to > a .dat file ,but I do not know how to control the digital, for > example: > > the mathematica code is > > f[x_]:=NIntegrate[Sin[y]/y,{y,0,x}]; > OpenWrite["xx.dat",FormatType->TextForm]; > Do[Write["xx.dat",N[x,4]," ",N[f[x],4]],{x,0.001,1,0.05}]; > Close["xx.dat"]; > > then in xx.dat you can find > 0 0. > 0.05 0.0499931 > 0.1 0.0999445 > 0.15 0.149813 > 0.2 0.199556 > 0.25 0.249134 > . > . > but I want it can be written in the form > 0.000 0.000 > 0.050 0.050 > 0.100 0.100 > 0.150 0.150 > 0.200 0.200 > 0.250 0.249 > . > . > It will be look much better! Use *NumberForm* to get a precise control over how many significant figures are displayed, how many digits after the decimal point, or padding characters and the like. f[x_] := NIntegrate[Sin[y]/y, {y, 0, x}]; OpenWrite["xx.dat", FormatType -> TextForm]; Do[Write["xx.dat", NumberForm[x, {3, 3}], " ", NumberForm[f[x], {3, 3}]], {x, 0.001, 1, 0.05}]; Close["xx.dat"]; will write 0.001 0.001 0.051 0.051 0.101 0.101 0.151 0.151 0.201 0.201 0.251 0.250 0.301 0.299 0.351 0.349 ... ... Regards, -- Jean-Marc