comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » floats from strings... duh!
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
floats from strings... duh! [message #4362] Fri, 26 May 1995 00:00 Go to next message
pjclinch is currently offline  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 #4418 is a reply to message #4362] Wed, 31 May 1995 00:00 Go to previous messageGo to next message
velt is currently offline  velt
Messages: 19
Registered: June 1994
Junior Member
In article <3q4tku$iqu@dux.dundee.ac.uk>, pjclinch@dux.dundee.ac.uk (Pete Clinch) writes:
>
>
> Rather than write my own function to convert "1.234" to 1.234, is there a
> way in the system to do it easily?

Try:

IDL> help,float('1.234')
<Expression> FLOAT = 1.23400

Robert Velthuizen (velt@rad.usf.edu),
Digital Medical Imaging Program of the
H. Lee Moffitt Cancer Center and Research Institute at the
University of South Florida, Tampa FL 33612.
Re: floats from strings... duh! [message #4437 is a reply to message #4362] Tue, 30 May 1995 00:00 Go to previous messageGo to next message
rarback is currently offline  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 Go to previous messageGo to next message
sterner is currently offline  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 #47845 is a reply to message #4362] Wed, 08 March 2006 18:42 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Kenneth P. Bowman writes:

> Doesn't COBOL provide fixed point arithmetic?

COBAL!? Didn't Pink Floyd just release Dark Side of the Moon?

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Floats [message #47846 is a reply to message #4362] Wed, 08 March 2006 18:24 Go to previous messageGo to next message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <1141851131.839027.320870@v46g2000cwv.googlegroups.com>,
"Sheldon" <shejo284@gmail.com> wrote:

> Hello,
>
> 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.
>
> Thanks,
> Sheldon

Doesn't COBOL provide fixed point arithmetic?

Ken Bowman
Re: Floats [message #47851 is a reply to message #4362] Wed, 08 March 2006 16:33 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  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 #47852 is a reply to message #4362] Wed, 08 March 2006 16:19 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Mark Hadfield writes:

>> May I ask why? Usually this sort of thing is required for printing, but
>> not for regular old storage of numbers.
>
> Currency?

This is the reason I prefer FIX to ROUND. It creates
a larger fractional part, which I route directly to my
own bank account. It's not much, but since no one wants
to hire me for my programming or teaching skills, it's
a living. :-(

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Floats [message #47854 is a reply to message #4362] Wed, 08 March 2006 15:59 Go to previous messageGo to next message
Mark Hadfield is currently offline  Mark Hadfield
Messages: 783
Registered: May 1995
Senior Member
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?

--
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 #47856 is a reply to message #4362] Wed, 08 March 2006 13:54 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
Sheldon wrote:
> Hello,
>
> 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.

paulv


--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
Re: Floats [message #47857 is a reply to message #4362] Wed, 08 March 2006 13:42 Go to previous messageGo to next message
Liberum is currently offline  Liberum
Messages: 48
Registered: September 2005
Member
I see what you are saying and my thoughts did venture down this road
before. I was mostly curious if it was at possible to control the
precision another way.

Much obliged!
Sheldon
Re: Floats [message #47858 is a reply to message #4362] Wed, 08 March 2006 13:42 Go to previous messageGo to next message
Liberum is currently offline  Liberum
Messages: 48
Registered: September 2005
Member
I see what you are saying and my thoughts did venture down this road
before. I was mostly curious if it was at possible to control the
precision another way.

Much obliged!
Sheldon
Re: Floats [message #47860 is a reply to message #4362] Wed, 08 March 2006 13:06 Go to previous messageGo to next message
Mark Hadfield is currently offline  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 Go to previous messageGo to next message
David Fanning is currently offline  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 Go to previous message
Andrew Cool is currently offline  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 #47981 is a reply to message #47845] Thu, 09 March 2006 05:11 Go to previous message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <MPG.1e794407722fe0e5989bcc@news.frii.com>,
David Fanning <davidf@dfanning.com> wrote:

> Kenneth P. Bowman writes:
>
>> Doesn't COBOL provide fixed point arithmetic?
>
> COBAL!? Didn't Pink Floyd just release Dark Side of the Moon?
>
> Cheers,
>
> David

No, COBOL was already old when Dark Side was released.

Obviously there was insufficient irony in my previous post (... how to
convey tone of voice?).

Ken
Re: Floats [message #47985 is a reply to message #47854] Thu, 09 March 2006 03:10 Go to previous message
James Kuyper is currently offline  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.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: matching 2 grids
Next Topic: Solaris 10

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:53:11 PDT 2025

Total time taken to generate the page: 0.00833 seconds