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

Adding Leading Zeroes to a Character String

Ernie_P_1
Beginner
759 Views
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 Kudos
1 Solution
Steven_L_Intel1
Employee
759 Views
WRITE (charNUM,'(I3.3)') NUM

View solution in original post

0 Kudos
3 Replies
Steven_L_Intel1
Employee
760 Views
WRITE (charNUM,'(I3.3)') NUM
0 Kudos
Ernie_P_1
Beginner
759 Views
WRITE (charNUM,'(I3.3)') NUM

Wow that was simple.

Thanks, Steve.
0 Kudos
Steven_L_Intel1
Employee
759 Views
... and standard conforming, unlike whatever it was you were using (never saw that extension before.)
0 Kudos
Reply