Silverfrost Forums

Welcome to our forums

WRITE - Format of REALs

11 Sep 2017 5:31 #20190

Hello, everyone,

Is there a way to get 0.296296238899 to be displayed as 2.962E-1 instead of 0.296E+00

What I'm using now is

WRITE(*,'(E10.3)')0.296296238899

Thanks!

11 Sep 2017 5:40 #20191

Use ES10.3 instead of E10.3 in the format. Note also that the literal constant that you showed has far more digits than can be represented in a single-precision real (between 7 and 8 significant digits).

11 Sep 2017 6:02 #20192

Thanks, mecej4.

All REAL variables in my program are of double precision.

But in some cases (as this one), 3 displayed digits after the decimal point are enough. Technically, this specific variable needn't be double, but I refrain from using different KINDs so I don't get them mixed up somewhere along the way.

11 Sep 2017 7:54 #20194

Is there also a way to exclude trailing zeros?

11 Sep 2017 8:21 #20196

Quoted from viroxa All REAL variables in my program are of double precision.

Technically, this specific variable needn't be double, but I refrain from using different KINDs so I don't get them mixed up somewhere along the way.

There were no variables in the WRITE statement that you showed. You had a single-precision real constant. Whether other variables (in the parts of the program that you did not show) are double precision, integer, etc., is not at all relevant to the nature of the WRITE statement.

11 Sep 2017 8:30 #20197

Quoted from viroxa Is there also a way to exclude trailing zeros?

No, and there are good reasons why that should not be done. First of all, it is assumed that when the programmer specifies Ew.d, the number of digits to display after the decimal point is exactly d, not some number ⇐ d. Secondly, when you see the string 3.1400, you know immediately that the number is not Pi. If you replace the 0s by blanks, the number could be Pi. See the problem? Thirdly, the output lines may be ugly because the decimal points in successive lines in a table printed under such a format would not be lined up.

That said, you can always use an internal write to a character string, replace zeros by blanks in that string, and then write the string. You take responsibility, not the Fortran runtime.

11 Sep 2017 8:36 #20198

Thanks! Now I see a little bit clearer again.

Please login to reply.