2

Consider the following program

program foo
    implicit none
    real :: pi
    pi = 3.1415923
    print*, "The value of pi is", pi
end program foo

This gives the output as

The value of pi is 3.1415923

But I want the variable to be displayed only up to 2 decimal places, so I tried

print'(f4.2)', "The value of pi is", pi

This throws me an error.

Fortran runtime error: Expected REAL for item 1 in formatted transfer, got CHARACTER                                                                                                                                       
(f4.2)  

How do I achieve the following output

The value of pi is 3.14
1
  • 1
    It is always good to show the error and not just write "an error". If you want your self-answered question to be helpful to others you want them to be able to search for the error message. Commented Mar 5, 2017 at 13:56

1 Answer 1

3

I achieved the desired output with the following

write(*,"(A)",advance="no") "The value of pi is " 
write(*,'(f4.2)') pi

or with print as (thanks to high performance Mark)

print'(a,f5.2)', "The value of pi is", pi
Sign up to request clarification or add additional context in comments.

1 Comment

Or write(*,'(a,f4.2)') "The value of pi is ", pi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.