View previous topic :: View next topic |
Author |
Message |
viroxa
Joined: 28 Jul 2017 Posts: 78
|
Posted: Mon Sep 11, 2017 6:31 pm Post subject: WRITE - Format of REALs |
|
|
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
Code: | WRITE(*,'(E10.3)')0.296296238899 |
Thanks! |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1885
|
Posted: Mon Sep 11, 2017 6:40 pm Post subject: |
|
|
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). |
|
Back to top |
|
|
viroxa
Joined: 28 Jul 2017 Posts: 78
|
Posted: Mon Sep 11, 2017 7:02 pm Post subject: |
|
|
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. |
|
Back to top |
|
|
viroxa
Joined: 28 Jul 2017 Posts: 78
|
Posted: Mon Sep 11, 2017 8:54 pm Post subject: |
|
|
Is there also a way to exclude trailing zeros? |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1885
|
Posted: Mon Sep 11, 2017 9:21 pm Post subject: Re: |
|
|
viroxa wrote: | 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. |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1885
|
Posted: Mon Sep 11, 2017 9:30 pm Post subject: Re: |
|
|
viroxa wrote: | 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. |
|
Back to top |
|
|
viroxa
Joined: 28 Jul 2017 Posts: 78
|
Posted: Mon Sep 11, 2017 9:36 pm Post subject: |
|
|
Thanks! Now I see a little bit clearer again. |
|
Back to top |
|
|
|