Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software Development Tools (Compilers, Debuggers, Profilers & Analyzers)
- Intel® Fortran Compiler
- How to add leading zero and suppress trailing zeroes when writing real number?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Bond__Andrew

New Contributor I

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
08:22 AM

139 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.

Link Copied

Accepted Solutions

FortranFan

Honored Contributor I

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
09:39 AM

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.
```

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:

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

6 Replies

Arjen_Markus

Valued Contributor III

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
08:59 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
09:35 AM

111 Views

FortranFan

Honored Contributor I

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
09:39 AM

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.
```

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:

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
09:50 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-16-2021
09:52 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-18-2021
07:46 AM

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!

For more complete information about compiler optimizations, see our Optimization Notice.