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

Home » Public Forums » archive » Controlling number of decimal places
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
Controlling number of decimal places [message #40207] Tue, 27 July 2004 15:38 Go to next message
i-kant is currently offline  i-kant
Messages: 14
Registered: June 2004
Junior Member
Say I have a DOUBLE variable that holds 200.23234 for example. I want
to output this number to a text file as a number rounded to two
decimal places. In other words, I want to output to a text file
"200.23".

How do I do this? I've looked into the ROUND function but only rounds
to the nearest integer.


Thanks for any help!



Edwin
Re: Controlling number of decimal places [message #40272 is a reply to message #40207] Thu, 29 July 2004 13:59 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Edwin writes:

> Sorry I'm still having trouble with this. I didn't fully explain my
> problem.
>
> The complete situation is that I have two DOUBLE variables. Say A and
> B, each holding a number with 5 decimal places, ex. 200.52343
>
> I want to output these two numbers rounded to two decimal places to a
> text file using PRINTF in the following format (with the comma):
>
> A,B
>
> So If my numbers were 560.34345 and 780.93434, I want my text file to
> read:
>
>
> 560.34,780.93
>
>
>
> David, I have tried your suggestion about the PRINTF format codes, but
> I am having trouble getting it to work:
>
> I can either write:
>
> printf, 1, A, FORMAT='(F8.2)'
> printf, 1, ','
> printf, 1, B, FORMAT='(F8.2)'
>
> which leads to all three entries being on separate lines.
>
> or I can write:
>
> printf, 1, A, ',', B, FORMAT='(F8.2)'
>
> which also doesn't work since the FORMAT code doesn't know what to do
> with the comma in between.
>
> The only solution I can think of is not to use FORMAT codes but to use
> some kind of rounding up function...Any ideas?

How about this:


IDL> x=560.34345
IDL> y=780.93434
IDL> Print, x, y, format='(2(f6.2,:,","))'
560.34,780.93

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Controlling number of decimal places [message #40275 is a reply to message #40207] Thu, 29 July 2004 13:40 Go to previous messageGo to next message
i-kant is currently offline  i-kant
Messages: 14
Registered: June 2004
Junior Member
Sorry I'm still having trouble with this. I didn't fully explain my
problem.

The complete situation is that I have two DOUBLE variables. Say A and
B, each holding a number with 5 decimal places, ex. 200.52343

I want to output these two numbers rounded to two decimal places to a
text file using PRINTF in the following format (with the comma):

A,B

So If my numbers were 560.34345 and 780.93434, I want my text file to
read:


560.34,780.93



David, I have tried your suggestion about the PRINTF format codes, but
I am having trouble getting it to work:

I can either write:

printf, 1, A, FORMAT='(F8.2)'
printf, 1, ','
printf, 1, B, FORMAT='(F8.2)'

which leads to all three entries being on separate lines.

or I can write:

printf, 1, A, ',', B, FORMAT='(F8.2)'

which also doesn't work since the FORMAT code doesn't know what to do
with the comma in between.

The only solution I can think of is not to use FORMAT codes but to use
some kind of rounding up function...Any ideas?
Re: Controlling number of decimal places [message #40285 is a reply to message #40207] Wed, 28 July 2004 13:58 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
David Fanning wrote:
> Edwin writes:
>
>
>> I guess you mean that data is the DOUBLE variable. If so, can you
>> explain what F8.2 means? I'm guessing the 2 determines 2 decimal
>> places, but what about the rest of it.
>
>
> Yes, the 2 determines the two decimal places. To learn more
> about format codes, type this at your IDL prompt:
>
> IDL> ? format codes

Wow. I didn't know you could do that. I tried it and got a little dialog box telling me:

"The topic "FORMAT CODES" does not exist. Valid topic names consist of any
IDL routine, object, command, statement, or system variable name.

The IDL Reference Guide will be displayed."

Huh. An *unsearchable* Reference Guide, mind. snuckinfessinrottenrasterdly acrobat reader
on linux.

paulv
Re: Controlling number of decimal places [message #40286 is a reply to message #40207] Wed, 28 July 2004 13:31 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Edwin writes:

> I guess you mean that data is the DOUBLE variable. If so, can you
> explain what F8.2 means? I'm guessing the 2 determines 2 decimal
> places, but what about the rest of it.

Yes, the 2 determines the two decimal places. To learn more
about format codes, type this at your IDL prompt:

IDL> ? format codes

Cheers,

David

--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http:/www.dfanning.com/
Phone: 970-221-0438, IDL Book Orders: 1-888-461-0155
Re: Controlling number of decimal places [message #40442 is a reply to message #40272] Wed, 04 August 2004 09:08 Go to previous message
i-kant is currently offline  i-kant
Messages: 14
Registered: June 2004
Junior Member
David Fanning <davidf@dfanning.com> wrote in message news:<MPG.1b7312f9e76268969897f0@news.frii.com>...
> Edwin writes:
>
>> Sorry I'm still having trouble with this. I didn't fully explain my
>> problem.
>>
>> The complete situation is that I have two DOUBLE variables. Say A and
>> B, each holding a number with 5 decimal places, ex. 200.52343
>>
>> I want to output these two numbers rounded to two decimal places to a
>> text file using PRINTF in the following format (with the comma):
>>
>> A,B
>>
>> So If my numbers were 560.34345 and 780.93434, I want my text file to
>> read:
>>
>>
>> 560.34,780.93
>>
>>
>>
>> David, I have tried your suggestion about the PRINTF format codes, but
>> I am having trouble getting it to work:
>>
>> I can either write:
>>
>> printf, 1, A, FORMAT='(F8.2)'
>> printf, 1, ','
>> printf, 1, B, FORMAT='(F8.2)'
>>
>> which leads to all three entries being on separate lines.
>>
>> or I can write:
>>
>> printf, 1, A, ',', B, FORMAT='(F8.2)'
>>
>> which also doesn't work since the FORMAT code doesn't know what to do
>> with the comma in between.
>>
>> The only solution I can think of is not to use FORMAT codes but to use
>> some kind of rounding up function...Any ideas?
>
> How about this:
>
>
> IDL> x=560.34345
> IDL> y=780.93434
> IDL> Print, x, y, format='(2(f6.2,:,","))'
> 560.34,780.93
>
> Cheers,
>
> David



Thanks david.

works now. :)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Programatically create structure
Next Topic: Displaying fluid flow in porous medium

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

Current Time: Fri Oct 10 11:13:14 PDT 2025

Total time taken to generate the page: 2.00214 seconds