Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28434 Discussions

How to add leading zero and suppress trailing zeroes when writing real number?

Bond__Andrew
New Contributor I
1,644 Views

Can anyone help me, please?

I am trying to produce formatted output from a Fortran program that matches existing output from a a C++ program. In particular I have a set of real numbers, as follows:

real Fx, Fy, Fz, x, y
Fx = 1000.0
Fy = 0.0
Fz = 120.5

The output I want is spaced-delimited, as follows:

1000 0 120.5

Using the following Fortran statement:

write(file, '(3(f0.1, x)') Fx, Fy, Fz

the output I get is:

1000.0 .0 120.5

My questions are:

  • How do I add the leading zero in front of the value for Fy?
  • How do I suppress the training zero from Fx (and ideally the decimal point - although I could live with that if I have to)?

Note that the values of Fx, Fy, and Fz are unknown until run time.

As a C++ programmer most of the time, I am struggling to do this in Fortran.

Any help would be much appreciated.

Thanks in advance.

0 Kudos
1 Solution
FortranFan
Honored Contributor II
1,615 Views
@Bond__Andrew:
As a C++ programmer most of the time, I am struggling to do this in Fortran.

Any help would be much appreciated.

@Bond__Andrew ,

What you are looking for are some of the additional fine-grained controls that have not quite made it into the language due to a number of reasons.

The one with leading zeroes is something the next language revision (Fortran 202X) is slated to provide.  But the one with trailing zeroes is missing, I'm afraid.

If this's of interest, a workaround you can consider is to write an extern "C" function in C++, given your background, and use it for your needs.  You can see here for an example for integer to string that you can adapt as per your needs:

https://fortran-lang.discourse.group/t/iso-c-binding-interface-to-a-c-function-returning-a-string/527/13?u=fortranfan.

By the way, since your inquiry is general Fortran, you may want to also ask at this Fortran Discourse site for additional viewpoints.

View solution in original post

6 Replies
Arjen_Markus
Honored Contributor I
1,625 Views

Fortran offers a lot of control over the output of numbers, but I am quite sure it does not give you control over the appearance of a leading zero or the suppression of trailing zeroes or decimal points.

If you really need this, then the way forward would be to:

  • Write the number to a string using a suitable format (G0 for instance)
  • Manipulate the appearance in the resulting string
  • Write out the string

Something along these lines:

write(*,*) formcpp(fx), formcpp(fy), formcpp(fz)

where the function formcpp uses an internal write to convert the value to a string. Whether it is worth the extra work, is up to you.

Steve_Lionel
Honored Contributor III
1,616 Views

I'll comment that the next revision of the Fortran standard is likely to contain a new feature to give you control over leading zeros in formatted output. It doesn't help today, but it will come.

Bond__Andrew
New Contributor I
1,608 Views

"... the next revision of the Fortran standard is likely to ... give you control over leading zeros in formatted output. It doesn't help today, but it will come."

Thanks for this, Steve.

0 Kudos
Bond__Andrew
New Contributor I
1,609 Views

Markus

Thanks for the quick reply. I feared as much.

Unfortunately I need to reproduce the exact format to allow automated comparison of results produced by two separate programs (one written in Fortran, one in C++).

However, your proposed solution looks like it could be made to work ... thanks for suggesting it.

Andrew

0 Kudos
FortranFan
Honored Contributor II
1,616 Views
@Bond__Andrew:
As a C++ programmer most of the time, I am struggling to do this in Fortran.

Any help would be much appreciated.

@Bond__Andrew ,

What you are looking for are some of the additional fine-grained controls that have not quite made it into the language due to a number of reasons.

The one with leading zeroes is something the next language revision (Fortran 202X) is slated to provide.  But the one with trailing zeroes is missing, I'm afraid.

If this's of interest, a workaround you can consider is to write an extern "C" function in C++, given your background, and use it for your needs.  You can see here for an example for integer to string that you can adapt as per your needs:

https://fortran-lang.discourse.group/t/iso-c-binding-interface-to-a-c-function-returning-a-string/527/13?u=fortranfan.

By the way, since your inquiry is general Fortran, you may want to also ask at this Fortran Discourse site for additional viewpoints.

Bond__Andrew
New Contributor I
1,549 Views

Thanks to all those who have replied to my post. This has been very helpful.

The solution I am pursuing at the moment is something like this:

call FormatReal(Fx, w, d)
write(file, '("Value of Fx = ", f<w>.<d>)') Fx

The subroutine FormatReal uses the value of Fx to determine appropriate values for w and d, whioch then control the width of the f format.

The allows me to add a leading 0, removed trailing zeroes, but not, unfortunately, get rid of the decimal point when appropriate.

This is close enough for my current purposes.

And I can see a full solution by adding a callback function from C/C++.

Thanks to all!

0 Kudos
Reply