1

For example, if I make these definitions:

Real, parameter :: No_01 = 2.34
Real, parameter :: No_02 = 34.56

and I want to write these variables with F format in this way:

Character(*), parameter :: FMT_01 = '(2x,F4.2)'
Character(*), parameter :: FMT_02 = '(2x,F5.2)'

The result of writing to screen would be:

Write(*, FMT_01 ) NO_01 => 2.34
Write(*, FMT_02 ) NO_02 => 34.56

Is there any kind of F format which can be used for geting this result of writing instead: Result is:

!Correct result          !Not correct result
002.340 Something         2.340 Something
034.560 Something         34.560 Something

The answers to How to pad floating point output with leading zeros? are not applicable, because the values can be negative.

  • 1
    Is this the same? https://stackoverflow.com/questions/17886390/how-to-pad-fortran-floating-point-output-with-leading-zeros – Vladimir F Героям слава Jan 02 '18 at 18:58
  • What is the rule for having a zero there? Is it always added, do you want exactly one zero (including when magnitude less than 1), or do you want a particular field width? (etc.) – francescalus Jan 02 '18 at 19:18
  • For example, if i got, in first iteration, result `2.34` with format `FMT_01`, and if i got, in second iteration, result `34.56` in output file i will get this: `****`. I know that result is in this interval `0.00` < result < `100.00` and if i want to avoid the `****` record i must make format definitions for every single iteration. Sugestion for using one format for all iterations and non-positive values? –  Jan 02 '18 at 19:25
  • If we know exactly that value is, for any calculation, in interval `0.00 < result < 101.00` how to make format which can avoid any asteriks and creating format for every single result of calculation? For example, how to print, with unique format, this three results: `002.540` , `029.334` , `100.487` –  Jan 02 '18 at 20:05
  • Did you look at the link? https://stackoverflow.com/questions/17886390/how-to-pad-fortran-floating-point-output-with-leading-zeros Is it the same or not? Please do answer, otherwise your question can become closed soon. – Vladimir F Героям слава Jan 02 '18 at 20:40
  • @VladimirF It is not the same issues because what if the value is negativ? –  Jan 02 '18 at 20:48
  • @HighPerformanceMark Yes it is. –  Jan 02 '18 at 20:50
  • simple enough to handle the neg case, just do `abs(areal-int(areal))` and be sure the integer format leaves room for the sign. – agentp Jan 02 '18 at 22:00
  • @agentp Can you write format for your solution? –  Jan 02 '18 at 22:07
  • It is not just a format, agentp's comment is a follow-up to this answer https://stackoverflow.com/a/17888581/721644 Have you read it? I put the link here twice in the hope you would read it. There is no simple format, you must do some additional coding as shown there and in agentp's comment. – Vladimir F Героям слава Jan 02 '18 at 22:49
  • @agentp Seems the OP is not getting it, but you and the linked answer have given the solution. Why not put your comments in an answer? – Matt P Jan 03 '18 at 23:48

3 Answers3

1

If you use F0.2 for both then you'll get:

  2.34
  34.56

Is that acceptable?

Steve Lionel
  • 6,972
  • 18
  • 31
  • It is not acceptable because i want to reserve always the same lenght for a writing format in large `Output.txt` without movings rows in that file for results in two different iteration. For example: `Iteration 1: Result is: 02.34` `Iteration 2: Result is: 34.56` –  Jan 02 '18 at 20:14
  • Can you check my answer post please? –  Jan 04 '18 at 09:51
0

For a constant field width without leading zeros, you could just specify a format with an assumed maximum field width. The negative signs are automatically handled, and the values are right-adjusted by default. For example:

character(len=6) :: form
real :: rv1, rv2

! Format: floating point, field width=8 (includes the "."), decimal places=2
form = '(f8.2)'  
rv1  = -12.34
rv2  =  123.45

write(*,form) rv1
write(*,form) rv2

! example output. notice the two leading spaces are just blanks, but the 
! field width ("8") is constant and the ("2") decimal places are aligned:
  -12.34
  123.45

If you want to have a constant field width with leading zeros, then you must use the technique shown at this SO answer you've already linked to in your question, along with the trick suggested by @agentp in a comment. I can't tell if you quite understood, so here's a subroutine demonstrating these ideas:

subroutine leadingzeros(rv)
    real, intent(in) :: rv
    ! local vars
    character(len=11) :: form

    if (int(rv)>0) then
        form='(i4.4,f0.2)'   ! allow a total of 4 leading zeros.
    else
        form='(i4.3,f0.2)'   ! "-" sign takes up one space, so 3 leading zeros remain.
    endif

    ! handle negative values as suggested by agentp
    write(*,form) int(rv), abs(rv-int(rv))
end subroutine leadingzeros

Now, when you call leadingzeros the output looks like:

! example output from 'call leadingzeros(rv1)'
-012.34
! example output from 'call leadingzeros(rv2)'
0123.45
Matt P
  • 2,287
  • 1
  • 11
  • 26
0

There is two methods for getting result from question:

Program Main

Implicit none

Open(15,File='Output.txt')

  Write(15,'(1x,a,1x,"j",1x,a,1x,"Juhu!")') Writing_01(67.45),Writing_01(-4.04)
  Write(15,'(1x,a,1x,"j",1x,a,1x,"Juhu!")') Writing_02(67.45),Writing_02(-4.04)

Close(15)

Contains

Function Writing_01 ( Deg ) Result ( Str )

Real,intent(in) :: Deg
Character(:),allocatable :: Str
Character(len = 15 ) :: Str_temp

If ( int( Deg ) > 0 ) then

    Write(Str_temp , '(F0.2)' ) 100000.0 + Deg
    Str_temp = Str_temp(2:)

Else

    Write(Str_temp, '(F0.2)' ) 100000.0 + abs(Deg)
    Str_temp = "-"//Str_temp(3:)

Endif

Str = trim ( adjustl ( Str_temp ))

End Function Writing_01

Function Writing_02 ( Deg ) Result ( Str_temp )

Real,intent(in) :: Deg
Character(:),allocatable :: Str_temp
Character(len=1561) :: Form_02 , Res

If (int( Deg ) > 0 ) then

  Form_02 = '(i5.5,f0.2)'   ! allow a total of 4 leading zeros.

Else

  Form_02 = '(i5.4,f0.2)'   ! "-" sign takes up one space, so 3 leading zeros remain.

Endif

Write(Res , Form_02 )  int( Deg ), abs( Deg - int( Deg ) )

Str_temp = trim ( adjustl ( Res ))

End Function Writing_02

End program Main
  • I just post the code which can give the wanted result and that is my only intention afther combining different sugestion nearly the final solution. That is all! –  Jan 04 '18 at 17:57
  • It is really not a problem to write your own solution, even if you include parts of other answers. However, I think the proper etiquette is to say that is what you are doing, and if you want to acknowledge the help you've received then you might accept/upvote the helpful answers. But that is entirely up to you and you don't need to listen to me at all. – Matt P Jan 04 '18 at 21:28
  • @Matt P Thanks for sugestion but i think that is clearly, from every singl post, that i have only intention to learn Fortran because i am new in that language. I am not interested in any other aspect - just only learning things and tricky details! I will delete my post in answer. –  Jan 04 '18 at 21:35
  • It's up to you ;) – Matt P Jan 04 '18 at 21:39