Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29251 Обсуждение

Adding Leading Zeroes to a Character String

Ernie_P_1
Начинающий
772Просмотр.
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
Сотрудник
772Просмотр.
3 Ответы
Steven_L_Intel1
Сотрудник
773Просмотр.
WRITE (charNUM,'(I3.3)') NUM
Ernie_P_1
Начинающий
772Просмотр.
WRITE (charNUM,'(I3.3)') NUM

Wow that was simple.

Thanks, Steve.
Steven_L_Intel1
Сотрудник
772Просмотр.
... and standard conforming, unlike whatever it was you were using (never saw that extension before.)
Ответить