Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Formatting output

patuco
Beginner
793 Views
Hi everyone in the forum,
just a question because i have no idea how to format the output i need.
I have a REAL *8 and the output restrictions shows that it should be:
1.0000 D-08 if the value is less than 1
1.0000 if the value is [1,9]
1.0000 D+03 is value bigger than 9
and the other problem is whith other variable(type CHARACTER) which represents integers, e.g, 0.000000 1.0000000 2.000000; and i have to return only 0 1 2 .
Any suggestion about how to format the output?
Thanks
0 Kudos
6 Replies
patuco
Beginner
792 Views

Hi everyon in the forum,

according to the lat post to achieve my goal, i was checking how the command FORMAT works but i do not know how to restrict the values.

One of the examples i have tryed :

WRITE(6,20) COM

FORMAT (I)

Message Edited by patuco on 04-25-200602:10 AM

0 Kudos
Les_Neilson
Valued Contributor II
792 Views

First of all FORMAT is not a "command".

It might be useful for you to think offormat as a "template" which is used by the write statement(s) to describe how the variables on the write statement are to be presented.

To takea simplified example as put in your first message you have your REAL*8 variable to print out:

real*8 rvar

.... code to set value of rvar somehow ...

if (rvar .lt 1.0d0) then

write (6,20) rvar

else if (rvar .ge. 1.0d0 .and. rvar .le. 9.0d0) then

write (6,21) rvar

else

write (6,22) rvar

endif

20 format(something suitable for numbers less than 1.0)

21 format(for numbers between 1 and 9)

22 format( for numbers greater than 9)

As for your second question. I am not sure I understand correctly. Do you have a character variable which contains the character representation of a real number? for example:

character*10 cvar

cvar = " 1.0000000"

if so then all you need to do is an "internal read" for example:

integer ivar

real rvar

read(cvar,"(F10.0)") rvar ! read the number from the character variable

ivar = nint(rvar) ! Get the nearest integer number to rvar and assign to ivar

hope this helps

Les

0 Kudos
patuco
Beginner
792 Views
Thanks for the explanation and the help Les,
it is very useful. Another problem is that I do not know how to limit the size of a real number in Fortran.
0 Kudos
Les_Neilson
Valued Contributor II
792 Views

Can you explainin moredetail what you mean "limit the size of a real number" ? What is the problem you are trying to solve?

Les

0 Kudos
patuco
Beginner
792 Views
Ok Les, here it goes
For example this is what the real contains:
1.119999964457385E-001
and i was wondering how can i limit this real, following the first post, to amaximum length like1.0000
I hope this make clearer my goal.
Thanks
0 Kudos
Les_Neilson
Valued Contributor II
792 Views

I assume you mean output the real number to a fileor to thescreen?

OK This is where formats apply. You should really read the online manual or get a Fortran book for the full explanation of the format Edit Descriptors (D,E,F,G for real numbers), but here is the simplified version :

real rvar

rvar = 1.119999964457385E-001

write(6,20) rvar

20 format(f10.4)

In the format (f10.4) the "10" says the size of the output is 10 characters in total, and the "4" says output with 4 decimal places. I this case the output would be " 0.1120"

10 characters totalwith 4leading spaces.

Example (F20.10) would be 20 characters total with 10 decimal places, and so on.

Les

0 Kudos
Reply