Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Bond__Andrew
New Contributor I
139 Views

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

Jump to solution

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

Accepted Solutions
FortranFan
Honored Contributor I
110 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/52....

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
Valued Contributor III
120 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
Black Belt Retired Employee
111 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.

FortranFan
Honored Contributor I
111 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/52....

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

Bond__Andrew
New Contributor I
104 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

Bond__Andrew
New Contributor I
103 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.

Bond__Andrew
New Contributor I
44 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!