number of decimals? [message #41751] |
Wed, 08 December 2004 20:49  |
Y.T.
Messages: 25 Registered: December 2004
|
Junior Member |
|
|
I've almost decided that the following is impossible in IDL, but I
figured I'd give it one more try...
I have a large dblarray of numbers -- say ten by ten million or so:
IDL> f = dblarr(10,1e7)
IDL> f = 1000.*randomn(seed,1e8)
(In reality these are real data values, not random numbers, of course).
I'd like to write these to a file (in the sense of a printf) with an
accuracy of two digits behind the decimal point. I COULD do something
like the following:
IDL> printf,unit,f,format='(10f9.2)'
but that'll introduce additional spaces wherever a number is smaller
than 10000. So a typical line might look like this:
123.45 678.23 1.23 12345.67 ...
etc
But what I want is
123.45 678.23 1.23 12345.67 ...
What I'm trying to do would be written in C somewhat like this: "%.2f
%.2f %.2f", i.e. a floating point number with two decimals.
The help tells me that I can specify the total width as zero to obtain
a "natural width" but if I do a "f0.2" in the above example, I get
things like
123.450000000 678.23000000 1.23000000 12345.6700000000 ...
I have looked into the %"... form of the 'format' keyword but that only
supports "%w.n" forms, not "%.n" forms.
For now, I am doing something unspeakably ugly like this:
printf,strcompress(string(f,format='(10f9.2)'))
which is reasonably workable as long as f is less than about 1/4 of my
total memory -- otherwise the various conversion start swapping and all
hell breaks loose.
I understand that I could do this simply line by line with strcompress,
but that takes approximately a metric forever.
Is there some trick or something that allows me to write a number with
two decimals and "just the right number of digits" before the decimal
point?
Thanks in advance...
cordially
Y.T.
--
Remove YourClothes before you email me.
|
|
|