How to defer Fortran floating point output with leading zeros?

I have some floating point numbers that I need to output from Fortran. Let's say the maximum number can be 999.9999, and they are all non-negative. I need zero padding before all numbers less than 100.

For example, if I have 25.6893782, 245.354567 and 1.2345678, I need to print them in a form similar to

025.6894 245.3546 001.2346 

How can i do this? It would be pretty easy with the edit descriptor T , if I knew that, for example, all numbers would be from 10 to 99, something like this. But I don’t know what it was before.

+8
floating-point formatting fortran
source share
4 answers

It works for me

 real :: areal 

then

 write(*,'(i3.3,f0.6)') int(areal),areal-int(areal) 
+11
source share

Zero-padding can be done for whole fields, so if you printed the result as two separate fields, you could do it. There is a method that is less than beautiful, 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 declared as CHARACTER*6 because it needs to hold the fractional part of your number (4 decimal places), the leading zero and the decimal point. This can be easily changed if you want to show more decimal places, although it would be harder if you wanted to show a variable number of decimal places.

The field descriptor I3.3 means "print an integer with a maximum field width of 3 and leave zeros on the left so that there are always 3 digits." When printing values, we take y(2:5) to remove the top zero.

Happy coding!

+2
source share

This is the trick I used at MS BASIC at Commodore PET in the late 70s. The code has been changed for negative numbers. If you want positive numbers to have a leading +, just change the last signchr character 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 - returns the 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 
+1
source share

I do not believe that there is an edit descriptor that does what you want, but you can mimic it with 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 
0
source share

All Articles