例如:
3.140000 简化为 3.14
3.00 简化为 3
3.040 简化为 3.04
3.040e30 简化为 3.04e30
program www_fcode_cn implicit none character( len = 52) :: c Do read(*,*) c call TrimStringNumber(c) write(*,*) Trim(c) End Do end program www_fcode_cn Subroutine TrimStringNumber( c ) !// Trim real String program !// Fortran Coder : Gao [ gao@fcode.cn ] !// http://www.fcode.cn character( Len = * ) :: c integer :: n integer :: i , j , k c = AdjustL( c ) n = Len_Trim( c ) i = index( c , "E" ) j = index( c , "e" ) k = max( i , j ) if ( k > 1 ) then !// 指数 j = index( c(:k-1) , "." , back = .true. ) if ( j < 1 ) return Do i = k-1 , j+1 , -1 if( c(i:i) /= '0' ) exit End Do if ( i /= j ) i = i + 1 c(i:n) = c(k:n) else !// 常规小数 j = index( c , "." , back = .true. ) if ( j < 1 ) return Do i = n , j+1 , -1 if( c(i:i) /= '0' ) exit End Do if ( i /= j ) i = i + 1 c(i:n) = ' ' end if End Subroutine TrimStringNumber