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

Adding Leading Zeroes to a Character String

Ernie_P_1
초급자
779 조회수
Hi,

I'm converting some old Fortran code and need toadd some leading zeroes to a character string. What I need to accomplish is:
I always need my numer(1-999) to have 3 character digits.
1 should be 001
50 should be 050
999 should be 999

My old code had the following internal write:
write(charNUM,"(B'###')")NUM

IVF doesn't allow this. My solution was to do the following:

IF(NUM.LT.10)THEN
WRITE(CHARNUM,'(I3)')NUM
CHARNUM='00'//TRIM(ADJUSTL(CHARNUM))
ELSE IF(NUM.LT.100)THEN
WRITE(CHARNUM,'(I3)')NUM
CHARNUM='0'//TRIM(ADJUSTL(CHARNUM))
END IF

There must be a cleaner solution than this. Any thoughts?

Thanks,
Ernie P.
0 포인트
1 솔루션
Steven_L_Intel1
779 조회수
WRITE (charNUM,'(I3.3)') NUM

원본 게시물의 솔루션 보기

0 포인트
3 응답
Steven_L_Intel1
780 조회수
WRITE (charNUM,'(I3.3)') NUM
0 포인트
Ernie_P_1
초급자
779 조회수
WRITE (charNUM,'(I3.3)') NUM

Wow that was simple.

Thanks, Steve.
0 포인트
Steven_L_Intel1
779 조회수
... and standard conforming, unlike whatever it was you were using (never saw that extension before.)
0 포인트
응답