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

Remove decimal separator with output format on real values in Fortran

Rhonda99
Beginner
1,493 Views

I need to change output format of real (double precision) values in an old Fortran program.

Currently, I have the following : WRITE(*, '(a, F4.0)') "My floating value : ", 12886849674.89

giving output My floating value : 12886849675.

I'd like to get rid of the ending ".", and show something looking like an integer rounding My floating value : 12886849675

But since I have a huge bunch of files and variables I'd like to modify this way, I'd like to do so by changing only format, without casting my double variable. Is that even possible and how?

0 Kudos
3 Replies
mecej4
Honored Contributor III
1,473 Views

You will need to specify what you want more completely and precisely. What is the range of numbers that you wish to cover? What do you intend to do with the output? What is wrong with writing one or more lines to a string, and editing that string as you wish (such as removing terminal decimal points)?

Your WRITE, as described, will print asterisks, not the integer that you mentioned, since F4.0 will not give you a field that is wide enough to represent a 11 digit integer. Without a 'D0' added to the literal constant in the I/O list, you will receive a number that is correct only in the leading seven or eight digits.

Suppose the number had been 0.001288684967489d0. How would you have wanted it to be printed?

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,445 Views
character(len=100) :: temp ! len > worst case formatted output
integer :: point
...
WRITE(temp, '(a, F20.0)') "My floating value : ", 12886849674.89D0
temp = adjustl(temp) ! remove leading spaces
point = index(temp,'.') ! locate decimal point
if(point >=1) temp(point:) = " " ! only when found (could be error ****?)
write(*,'(a,a)') "My floating value : ", trim(temp) ! only the numbers

FWIW You may want to consider writing a function that takes in a float/double, performs the internal write, edits out the .nnn and returns the trimmed result.

Jim Dempsey

0 Kudos
Steve_Lionel
Honored Contributor III
1,457 Views

Fortran does not have a format edit descriptor that will do what you want. You could use F format to write to a string and then remove the trailing decimal.

0 Kudos
Reply