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

Remove decimal separator with output format on real values in Fortran

Rhonda99
Einsteiger
3.277Aufrufe

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 Antworten
mecej4
Geehrter Beitragender III
3.257Aufrufe

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?

jimdempseyatthecove
Geehrter Beitragender III
3.229Aufrufe
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

Steve_Lionel
Geehrter Beitragender III
3.241Aufrufe

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.

Antworten