floats from strings... duh! [message #4362] |
Fri, 26 May 1995 00:00  |
pjclinch
Messages: 27 Registered: May 1993
|
Junior Member |
|
|
I'm sure I'm missing something obvious... want to do the equivalent of a
C sscanf or atof on a string I've got in a PV~Wave program that has a
floating point number in string form.
Rather than write my own function to convert "1.234" to 1.234, is there a
way in the system to do it easily?
Thanks.
Pete.
--
Peter Clinch Dundee Teaching Hospitals NHS Trust
voice: 44 1382 660111 x 3637 snail: Directorate of Medical Physics
fax: 44 1382 640177 Ninewells Hospital
email: p.j.clinch@dundee.ac.uk Dundee DD1 9SY Scotland UK
|
|
|
|
Re: floats from strings... duh! [message #4437 is a reply to message #4362] |
Tue, 30 May 1995 00:00   |
rarback
Messages: 15 Registered: October 1992
|
Junior Member |
|
|
In article <PHIL.95May27160944@peace.med.ohio-state.edu>,
phil@peace.med.ohio-state.edu (Phil) writes:
> In article <3q568j$5bd@nntp.Stanford.EDU> zowie@banneker.stanford.edu
(Craig DeForest) writes:
>
>> which won't look right because the title will be "Plot number 5"
>> instead of "Plot number 5", so you need to tack another bag on that:
>>
>> plot,mydata,title="Plot number "+strtrim(string(plotno),2)
>>
>
> you could also just use the strcompress function. ie:
> plot,mydata,title="Plot number"+strcompress(plotno)
>
> There is also no need for the trailing space in the "Plot number"
> string since string compress prepends a space.
>
As long as we are all adding our tuppence to favorite ways to format strings:
plot, mydata, title = string( 'Plot number ', plotno, format='(a, i0'))
> Hope this helps.
Double, double!
----
Harvey Rarback phone: (312)702-9931
CARS fax: (312)702-5454
University of Chicago Internet: rarback@cars3.uchicago.edu
5640 South Ellis Avenue HEPnet/SPAN: 47583::RARBACK
Chicago, IL 60637 "There is no pizza in Sweden."
|
|
|
Re: floats from strings... duh! [message #4439 is a reply to message #4362] |
Tue, 30 May 1995 00:00   |
sterner
Messages: 106 Registered: February 1991
|
Senior Member |
|
|
zowie@banneker.stanford.edu (Craig DeForest) writes:
. . .
> plot,mydata,title="Plot number "+strtrim(string(plotno),2)
strtrim converts its argument to a string, so you can shorten the above to:
plot,mydata,title="Plot number "+strtrim(plotno,2)
It would be nice if the ,2 above were default.
Ray Sterner sterner@tesla.jhuapl.edu
The Johns Hopkins University North latitude 39.16 degrees.
Applied Physics Laboratory West longitude 76.90 degrees.
Laurel, MD 20723-6099
WWW Home page: ftp://fermi.jhuapl.edu/www/s1r/people/res/res.html
|
|
|
|
|
Re: Floats [message #47851 is a reply to message #4362] |
Wed, 08 March 2006 16:33   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Mark Hadfield wrote:
> Paul Van Delst wrote:
>
>> Sheldon wrote:
>>
>>>
>>> Here is a silly question: Can I define a float array and control how
>>> many decimal places are kept? For example, I want all values to only
>>> have an accuracy to the nearest 100th (20.15 and not 20.154983445).
>>> Kind of like in printing, you know, the f5.2 print definition, but only
>>> for variables and arrrays.
>>
>>
>> May I ask why? Usually this sort of thing is required for printing,
>> but not for regular old storage of numbers.
>
>
> Currency?
Ah, fair enough. But a hasty google reveals that currency calculations are not done at the
cent level - a higher precision is required; for currency conversions (6sigfigs), or in
calculating how much you have to pay when you buy petrol or gas etc, (1000th's seem to be
the common unit.).
Another example I looked at converted 1000 Finnish Markka's to Euros 6 times (conversion
rate to 6sigfig) and added the result (�1024.92). Converting 6000 Finnish Markka's was
�1024.90 A 2 cent difference. That may add up when you're converting lotsa moola lotsa times.
So, it seems to me the only reason you'd need precision to 100th's for currency is for
printing your invoices. :o)
cheers,
paulv
p.s. I can't believe I just read 20 pages from a report from the "EUROPEAN COMMISSION
DIRECTORATE GENERAL II ECONOMIC AND FINANCIAL AFFAIRS Monetary matters". Jeez. :o)
--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
|
|
|
|
|
|
|
|
Re: Floats [message #47860 is a reply to message #4362] |
Wed, 08 March 2006 13:06   |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
David Fanning wrote:
> Sheldon writes:
>
>> Here is a silly question: Can I define a float array and control how
>> many decimal places are kept? For example, I want all values to only
>> have an accuracy to the nearest 100th (20.15 and not 20.154983445).
>> Kind of like in printing, you know, the f5.2 print definition, but only
>> for variables and arrrays.
>
> [ Silly answer snipped ...]
>
> How about something like this:
>
> array = RandomU(-3L, 4, 5)
> print, array
>
> array = Fix(array*100) / 100.
> print, array
This will give you rounding towards 0. Normally you would be better off
with.
array = Round(array*100) / 100.
--
Mark Hadfield "Kei puwaha te tai nei, Hoea tahi tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: Floats [message #47861 is a reply to message #4362] |
Wed, 08 March 2006 13:00   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Sheldon writes:
> Here is a silly question: Can I define a float array and control how
> many decimal places are kept? For example, I want all values to only
> have an accuracy to the nearest 100th (20.15 and not 20.154983445).
> Kind of like in printing, you know, the f5.2 print definition, but only
> for variables and arrrays.
I think you should get one of those Macintosh computers.
I hear they are really friendly. They can probably do
this. The rest of them...they are about as intelligent
as a piece of silicon! :-)
How about something like this:
array = RandomU(-3L, 4, 5)
print, array
array = Fix(array*100) / 100.
print, array
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: Floats [message #47966 is a reply to message #47851] |
Thu, 09 March 2006 15:16  |
Andrew Cool
Messages: 219 Registered: January 1996
|
Senior Member |
|
|
Paul Van Delst wrote:
> Mark Hadfield wrote:
>> Paul Van Delst wrote:
>>
>>> Sheldon wrote:
>>>
>>>>
>>>> Here is a silly question: Can I define a float array and control how
>>>> many decimal places are kept? For example, I want all values to only
>>>> have an accuracy to the nearest 100th (20.15 and not 20.154983445).
>>>> Kind of like in printing, you know, the f5.2 print definition, but only
>>>> for variables and arrrays.
>>>
>>>
>>> May I ask why? Usually this sort of thing is required for printing,
>>> but not for regular old storage of numbers.
>>
>>
>> Currency?
>
> Ah, fair enough. But a hasty google reveals that currency calculations are not done at the
> cent level - a higher precision is required; for currency conversions (6sigfigs), or in
> calculating how much you have to pay when you buy petrol or gas etc, (1000th's seem to be
> the common unit.).
>
> Another example I looked at converted 1000 Finnish Markka's to Euros 6 times (conversion
> rate to 6sigfig) and added the result (1024.92). Converting 6000 Finnish Markka's was
> 1024.90 A 2 cent difference. That may add up when you're converting lotsa moola lotsa times.
>
> So, it seems to me the only reason you'd need precision to 100th's for currency is for
> printing your invoices. :o)
>
> cheers,
>
> paulv
>
When I were young'un just out of Uni, I worked in the local HQ of
British Petroleum (BP),
and the one number we all learned by heart was the conversion factor
from imperial
gallons to metric litres : 4.54609 litres/imp_gallon. A road going
petrol tanker in those
days held 37,000 litres, and a big service station might get 2 or 3 of
those delivered
per day. Even the temperature of the petrol (gas for the Yanks) was
taken into account
in determining the volume delivered. Of course 37,000L ain't all that
much compared to
the mega-millions held in the big storage tanks, where 4.54609 really
made a difference.
I had the opportunity once to walk on top of one of the big storage
tanks. Round and round up the spiral stairs running around the outside
oft he tank, then onto the roof.
The roof was made of thin metal, and designed to blow off in event of
fire. It flexed
like a trampoline as you walked on it, and it was the most uncanny, and
scariest
experience all at the same time. Would this thing support me, or dump
me into
the hydrocarbon bath below...? Once was enough!
Slightly OT, but Hey! It's Friday...
Andrew
|
|
|
|
Re: Floats [message #47985 is a reply to message #47854] |
Thu, 09 March 2006 03:10  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
Mark Hadfield wrote:
> Paul Van Delst wrote:
>> Sheldon wrote:
>>>
>>> Here is a silly question: Can I define a float array and control how
>>> many decimal places are kept? For example, I want all values to only
>>> have an accuracy to the nearest 100th (20.15 and not 20.154983445).
>>> Kind of like in printing, you know, the f5.2 print definition, but only
>>> for variables and arrrays.
>>
>> May I ask why? Usually this sort of thing is required for printing, but
>> not for regular old storage of numbers.
>
> Currency?
If you require exact results in cents, you shouldn't usea float number
representing the number of dollars, you should use a long number
representing the number of cents. You'll need to use a floating point
type for intermediate calculations if you're doing things like
computing compound interest. Keep in mind that FLOAT usually doesn't
have enough precision for such calculations: use DOUBLE instead.
However, if you want to display a long series of numbers, and print a
correct total for the displayed numbers, you'd better use an integer
type.
There are languages which provide direct support for fixed-point types.
They use an underlying integer type to represent a number with a fixed
number of digits after the decimal point. IDL isn't one of those
languages.
|
|
|