Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Leading Zeros for Real Numbers in Fortran 90?

713 views
Skip to first unread message

qqu...@hotmail.com

unread,
Apr 3, 2009, 5:29:59 AM4/3/09
to
Dear Everyone:

For leading zeros in integer numbers, we can use format such as "i4.4"
to produce such as "0037". Is there a similar way in Fortran 90 to
produce leading zeros in REAL numbers, such as 0045.28? Or do I have
to split the parts BEFORE and AFTER the decimal point and manipulate
them separately?

Thank you for reading and replying!

--Roland

Arjen Markus

unread,
Apr 3, 2009, 7:19:48 AM4/3/09
to

I do not think there is an edit descriptor for that, but here is
a small program that will do what you want:

program vv
real :: x = 45.28

character(len=20) :: string

write( string, '(f10.2)' ) x

string = repeat( '0', 10-len_trim(adjustl(string))) // adjustl
(string)

write( *, *) string

end program

The trick is:
- Write the number according to the format required
- Replace as many leading blanks as necessary by 0.

There are other ways too, but this is fairly compact (though not as
compact as a format) and you can easily put it in a function/
subroutine
(beware of recursive writes though)

Regards,

Arjen

John Keenan

unread,
Apr 3, 2009, 11:41:27 AM4/3/09
to
"Arjen Markus" wrote

> here is a small program that will do what you want:

for *positive* real values.


qqu...@hotmail.com

unread,
Apr 3, 2009, 3:02:03 PM4/3/09
to
On Apr 3, 7:19 am, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
>
> I do not think there is an edit descriptor for that, but here is
> a small program that will do what you want:
>
> program vv
>     real :: x = 45.28
>
>     character(len=20) :: string
>
>     write( string, '(f10.2)' ) x
>
>     string = repeat( '0', 10-len_trim(adjustl(string))) // adjustl
> (string)
>
>     write( *, *) string
>
> end program
>
> The trick is:
> - Write the number according to the format required
> - Replace as many leading blanks as necessary by 0.
>
> There are other ways too, but this is fairly compact (though not as
> compact as a format) and you can easily put it in a function/
> subroutine
> (beware of recursive writes though)
>
> Regards,
>
> Arjen


Arjen and John: Thank you for your replies and detailed code. I have
thought about other awkward methods, but the functions like "repeat"
and "adjustl" are not in the pool of the active functions in my mind.
Thank you again!

--Roland

Arjen Markus

unread,
Apr 6, 2009, 3:14:09 AM4/6/09
to
On 3 apr, 17:41, "John Keenan"

Hm, yes, you are right. I did not think of that. Including a sign
will make the code a bit more complicated ... The principle stays the
same.

Regards,

Arjen

robin

unread,
Apr 19, 2009, 7:53:26 PM4/19/09
to
"Arjen Markus" <arjen....@wldelft.nl> wrote in message
news:9bd32a1c-f1f7-4ae6...@z15g2000yqm.googlegroups.com...

> On 3 apr, 17:41, "John Keenan"
> <john.removeme.kee...@optimapowerware.com> wrote:
> > "Arjen Markus" wrote
> >
> > > here is a small program that will do what you want:
> >
> > for *positive* real values.
>
> Hm, yes, you are right. I did not think of that. Including a sign
> will make the code a bit more complicated ...

Only slightly more so, as the sign is in a fixed position
in the output.

Message has been deleted
Message has been deleted

fabric...@gmail.com

unread,
Jul 20, 2018, 10:37:35 AM7/20/18
to
This is an old thread, but I arrived there after meeting the same problem (10 years after the opening post!). I like Arjen's solution, for which I propose here a small improvement. It is now written as a reusable function, and negative numbers are supported. It is also possible to ask for the positive sign with an option. Any comment and bug fix is welcome (it was tested with gfortran 8.1.0).

Fabrice

program pad_number
real :: x = 1234.56
real :: y = -1234.56
write(*,*) trim( padded_number(x, '(f10.2)') )
write(*,*) trim( padded_number(x, '(f10.2)', .false.) )
write(*,*) trim( padded_number(x, '(f10.2)', .true.) )
write(*,*) trim( padded_number(y, '(f10.2)') )
stop

contains

function padded_number(x, format_string, always_show_sign) result(string)
real, intent(in) :: x
character(*), intent(in) :: format_string
logical, intent(in), optional :: always_show_sign ! .false. by default (if .true., show + for positive numbers)

character(len=256) :: string
character(len=1) :: sign
integer :: n

sign = ''
if (x < 0) then
sign = '-'
else
if (present(always_show_sign)) then
if (always_show_sign) sign = '+'
end if
end if

write(string, format_string) abs(x)
n = len_trim(string) - len_trim(sign)

string = sign &
// repeat( '0', n-len_trim(adjustl(string))) &
// adjustl(string)

string = adjustl(string)
end function

end program

robin....@gmail.com

unread,
Jul 20, 2018, 10:40:11 PM7/20/18
to
On Friday, April 3, 2009 at 8:29:59 PM UTC+11, qqu...@hotmail.com wrote:
> Dear Everyone:
>
> For leading zeros in integer numbers, we can use format such as "i4.4"
> to produce such as "0037". Is there a similar way in Fortran 90 to
> produce leading zeros in REAL numbers, such as 0045.28? Or do I have
> to split the parts BEFORE and AFTER the decimal point and manipulate
> them separately?

You could scale the values using an appropriate WRITE statement,
WRITing the values to a string.
Then move the decimal point.

X = 12.3456
WRITE (*, '(-4PF12.8)' ) X
WRITE (*, '(-4PF12.8)' ) -X
will produce
0.00123456
-0.00123456
0 new messages