## Fortran Format command

## Fortran Format command

(OP)

Hi,

I am having difficulty using the FORMAT statement to output numbers to a specified number of decimal places. The program is stated below and is a simple quadratic equation calculator.

If someone can help me it would be a great help.

Thanks in advance.

HW

PROGRAM OOV2

IMPLICIT NONE

REAL a, b, c, x1, x2

INTEGER case

WRITE (*,*) "Solving a Quadratic equation"

WRITE (*,*) "Enter the three coefficients: "

READ (*,100) a, b, c

100 FORMAT(3I1)

CALL quad(a,b,c,case,x1,x2)

SELECT CASE (case)

CASE (1)

WRITE (*,200) "The single root is:" ,x1

CASE (2)

WRITE (*,200) "The two real roots are " ,x1, " and " ,x2

CASE (3)

WRITE (*,200) "The real and imaginary parts are " ,x1, " and " ,x2

END SELECT

200 Format (F6.2)

CONTAINS

SUBROUTINE quad (a, b, c, case, x1, x2)

REAL, INTENT(IN)::a,b,c

INTEGER, INTENT(OUT)::case

REAL, INTENT(OUT)::x1,x2

REAL d

IF(a==0.0) THEN

case=1

x1=-c/b

RETURN

ENDIF

d=b*b-4.0*a*c

IF(d>=0.0) THEN

case=2

d=SQRT(d)

x1=(-b+d)/(2.0*a)

x2=(-b-d)/(2.0*a)

RETURN

ELSE

case=3

x1=-b/(2.0*a)

x2=SQRT(-d)/(2.0*a)

ENDIF

END SUBROUTINE

END PROGRAM OOV2

I am having difficulty using the FORMAT statement to output numbers to a specified number of decimal places. The program is stated below and is a simple quadratic equation calculator.

If someone can help me it would be a great help.

Thanks in advance.

HW

PROGRAM OOV2

IMPLICIT NONE

REAL a, b, c, x1, x2

INTEGER case

WRITE (*,*) "Solving a Quadratic equation"

WRITE (*,*) "Enter the three coefficients: "

READ (*,100) a, b, c

100 FORMAT(3I1)

CALL quad(a,b,c,case,x1,x2)

SELECT CASE (case)

CASE (1)

WRITE (*,200) "The single root is:" ,x1

CASE (2)

WRITE (*,200) "The two real roots are " ,x1, " and " ,x2

CASE (3)

WRITE (*,200) "The real and imaginary parts are " ,x1, " and " ,x2

END SELECT

200 Format (F6.2)

CONTAINS

SUBROUTINE quad (a, b, c, case, x1, x2)

REAL, INTENT(IN)::a,b,c

INTEGER, INTENT(OUT)::case

REAL, INTENT(OUT)::x1,x2

REAL d

IF(a==0.0) THEN

case=1

x1=-c/b

RETURN

ENDIF

d=b*b-4.0*a*c

IF(d>=0.0) THEN

case=2

d=SQRT(d)

x1=(-b+d)/(2.0*a)

x2=(-b-d)/(2.0*a)

RETURN

ELSE

case=3

x1=-b/(2.0*a)

x2=SQRT(-d)/(2.0*a)

ENDIF

END SUBROUTINE

END PROGRAM OOV2

## RE: Fortran Format command

READ (*,*) a, b, c

as you appear to be reading them in as single integers.

To output the results with more decimal places just change statement 200 to say:-

200 Format (F15.6)

or better still use the G format instead:-

200 Format (G15.6)

and you will be able to output any value.

www.Roshaz.com

## RE: Fortran Format command

... I think you need to move your character strings from your WRITE statements to your FORMAT statement(s).

Thus:

WRITE(*,200)x1

200 FORMAT(" The single root is: ",F15.6)

Obviously you will then need more FORMAT statements.

## RE: Fortran Format command

or perhaps change the format to:-

200 Format (2(A,G15.6))

www.Roshaz.com

## RE: Fortran Format command

Thanks for your help.

I have tried both methods but with no success. Basically I am getting a four decimal place output when I am trying to specify a 2 decimal place.

I have attached the two methods that were suggested.

As far as I can see that is the method to format an output?

Best Regards,

William

## RE: Fortran Format command

If you want to force a 2 decimal place output then go back to the "F" format, but allow more space for larger numbers to be output, 6 characters as you had in F6.2 is seldom going to be enough.

Try:-

SELECT CASE (case)

CASE (1)

WRITE (*,200) "The single root is:", x1

CASE (2)

WRITE (*,200) "The two real roots are ", x1, " and ", x2

CASE (3)

WRITE (*,200) "The real and imaginary parts are ", x1, " and ", x2

END SELECT

200 Format (2(A,F15.2))

www.Roshaz.com

## RE: Fortran Format command

That is my problem solved. Is the 2 in the statement

200 Format (2(A,F15.2))

because I have two variables, x1 and x2?

Thanks again

## RE: Fortran Format command

Be aware however that F15.2 limits the range of values that can be displayed (whereas G15.2 will change to exponential format to cover values outside the range of F15.2)

www.Roshaz.com