ksbiefr
ksbiefr

Reputation: 63

Formatting in the write statement in Fortran

I try to use write statement in Fortran. But the required format is not generated. When I change the format

 write(unit=28, fmt='(1X,E15.7,E15.7,E15.7)')

to

 write(unit=28, fmt='(1X,E15.7,E15.7,I7)') 

it will give me there value as *******

     0.2375528E-01  0.3807880E-02        *******
     0.1294881E-01  0.7272966E-01        *******
     0.9220393E-02 -0.7748791E-01        *******
     0.3838744E-02 -0.1024217E+00        *******
     0.4709786E-02 -0.2939432E-01        *******

Main Code:

 post = post+1
 write(filename,'(a,i5.5,a)') 'particles', post, '.dat'
 open ( unit=28, file=filename , recl=300 )
 write(unit=28, fmt='(1X,E15.7,E15.7,E15.7)') (xp(i,:), i=1,npart)
 close ( unit=28 )

Result form the code

     0.2375528E-01  0.3807880E-02        0.1E+01
     0.1294881E-01  0.7272966E-01        0.2E+01
     0.9220393E-02 -0.7748791E-01        0.3E+01
     0.3838744E-02 -0.1024217E+00        0.4E+01
     0.4709786E-02 -0.2939432E-01        0.5E+01

Required result needed in this format

     0.2375528E-01  0.3807880E-02        1
     0.1294881E-01  0.7272966E-01        2
     0.9220393E-02 -0.7748791E-01        3
     0.3838744E-02 -0.1024217E+00        4
     0.4709786E-02 -0.2939432E-01        5

Can anybody suggest me how I can get the required result?

Upvotes: 1

Views: 999

Answers (2)

John Alexiou
John Alexiou

Reputation: 29244

Request zero decimals

write(unit=28, fmt='(1X,E15.7,E15.7,F15.0)') (xp(i,:), i=1,npart)

Upvotes: 0

Convert the numbers to integers

write(unit=28, fmt='(1X,E15.7,E15.7,I7)')  (xp(i,1:2),nint(xp(i,3)), i=1,npart)

Upvotes: 4

Related Questions