How to pad Fortran floating point output with leading zeros?

怎甘沉沦 提交于 2019-12-17 20:57:45

问题


I have some floating point numbers that I need to output from a Fortran program. Let's say the maximum number could be 999.9999 and they are all non-negative. I need zero-padding in front of all numbers less than 100.

For instance, if I have 25.6893782, 245.354567, and 1.2345678, I need to print them out in a form something like

025.6894
245.3546
001.2346

How can I do this? It would be fairly easy with the T edit descriptor if I knew that, for instance, all numbers would be between 10 and 99, something like that. But there is no way for me to know that ahead of time.


回答1:


This works for me

real :: areal

then

write(*,'(i3.3,f0.6)') int(areal),areal-int(areal)



回答2:


Zero-padding can be performed for integer fields, so if you printed the result as two separate fields you might be able to make it happen. Here's a way that is less than pretty, but works. Say x is the value you want to print:

DOUBLE PRECISION x
CHARACTER*6 y

x = 123.4567

WRITE(y,'(F6.4)') x-int(x)


WRITE(*,'(I3.3,A5)') int(x), y(2:5)

y is declared as a CHARACTER*6 because it needs to hold the fractional part of your number (4 decimal places), a leading zero, and a decimal point. This can be easily changed if you want to show more decimal places, though it would be trickier if you wanted to show a variable number of decimal places.

The I3.3 field descriptor means "print an integer with a maximum field width of 3 and pad left with zeroes so that there are always 3 digits". When printing out the value we take y(2:5) to strip off the leading zero.

Happy coding!




回答3:


This is a trick I used to use in MS BASIC on the Commodore PETs in the late 70s. The code has been modified for negative numbers. If you would like positive numbers to have a leading +, just change the last character of signchr to '+'

subroutine leadingzero(x)
   real x
   character(len=16) str
   character, dimension(3):: signchr = (/'-', ' ', ' ' /)
   write(str,'(F9.4)') 1000.0 + abs(x)
   write(*,*) signchr(int(sign(1.0,x)) + 2), str(2:) ! drop the 1
end subroutine leadingzero

program main
   call leadingzero(0.01)
   call leadingzero(0.1)
   call leadingzero(2.532)
   call leadingzero(9.9999)
   call leadingzero(9.999999)
   call leadingzero(10.987)
   call leadingzero(123.456)
   call leadingzero(0.0)
   call leadingzero(-0.01)
   call leadingzero(-0.1)
   call leadingzero(-2.532)
   call leadingzero(-9.9999)
   call leadingzero(-9.999999)
   call leadingzero(-10.987)
   call leadingzero(-123.456)
end program

Edit - returning result in a string

subroutine leadingzerostr(x, str_temp)
    real x
    character(*) str_temp
    character(len=10) str
    character, dimension(3):: signchr = (/'-', ' ', ' ' /)
    write(str,'(F10.4)') 10000.0 + abs(x)
    str_temp = str
    str_temp(1:1) = signchr(int(sign(1.0,x)) + 2)
end subroutine leadingzerostr



回答4:


I do not believe that there is an edit descriptor that does what you want, but you could simulate it with an if-statement:

if(var < 10) then
   write(*,'(a,f6.4)') '00',var
else if(var < 100) then
   write(*,'(a,f7.4)') '0',var
else
   write(*,'(f8.4)') var
endif


来源:https://stackoverflow.com/questions/17886390/how-to-pad-fortran-floating-point-output-with-leading-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!