Field calculator make 4 digit numbers

1073
6
Jump to solution
07-08-2013 06:05 AM
BobbySells
New Contributor II
I have object with a number from 1 to 1500.  I need to concatenate with another field but need all numbers to be 7 digits.  Field 1 is done and contains 3 numbers.  I need to add the object number field but have it as xxx0001 through xxx1500.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Try this:

Pre-logic:

def concatFields( field1, field2 ):   digits = str(field1)[-4:]   code = str(field2)   return int(code + digits)


Expression:

concatFields( !ObjectNums!, !Field1!)


I had mine set up where the "field1" parameter is the 7 digit object numbers and "field2" is your 3 digit code.  This worked for me.  Be sure to set the parser to Python.  See attached screenshots:

[ATTACH=CONFIG]25790[/ATTACH][ATTACH=CONFIG]25791[/ATTACH]

View solution in original post

0 Kudos
6 Replies
BobbySells
New Contributor II
This is what I have tried but of course errors.

def strID():
IF [ObjNum]<10
then strID()="100000"& [ObjNum]
elseif  [ObjNum]<100
then strID()="10000"& [ObjNum]
elseif  [ObjNum]<1000
then strID()="1000"& [ObjNum]
elseif  [ObjNum]<10000
then strID()="100"& [ObjNum]
end if
0 Kudos
by Anonymous User
Not applicable
Try this:

Pre-logic:

def concatFields( field1, field2 ):   digits = str(field1)[-4:]   code = str(field2)   return int(code + digits)


Expression:

concatFields( !ObjectNums!, !Field1!)


I had mine set up where the "field1" parameter is the 7 digit object numbers and "field2" is your 3 digit code.  This worked for me.  Be sure to set the parser to Python.  See attached screenshots:

[ATTACH=CONFIG]25790[/ATTACH][ATTACH=CONFIG]25791[/ATTACH]
0 Kudos
PhillipCobb
New Contributor
This should also work. "field1" would be the Object ID and "field2" would be the 3 digit number. create a new Text field then run.

Pre-Logic Script Code:

def concat(field1, field2):
  if field1 >= 0 and field1 < 10:
    object = str("000") + str(field1)
    return str(field2) + str(object)
  if OID > 9 and field1 < 100:
    object = str("00") + str(field1)
    return str(field2) + str(object)
  if OID > 99 and field1 < 1000:
    object = str("0") + str(field1)
    return str( field2) + str(object)
  else:
    object = str(field1)
    return  str(field2) + str(object)


Field =

concat( !FID!, !digit! )
0 Kudos
StacyRendall1
Occasional Contributor III
You can also use Python zero fill string operator. It adds a defined number of zeros to the start of a string. You can use it like this:
def concatFields(Field1, ObjNum):
  prefix = str(Field1)
  suffix = str(ObjNum).zfill(4)
  return int(prefix + suffix)
0 Kudos
DanPatterson_Retired
MVP Emeritus
This could be made into a field calculator expression
'''
PaddingStringsDemo.py
'''
vals = [1,10,100,1000]
for i in vals:
  out = "%08.2f" % (i)
  print str(out)

And this is the output...you will note it is a string representation of the number.
>>>
00001.00
00010.00
00100.00
01000.00
0 Kudos
by Anonymous User
Not applicable
I think I misunderstood the original question; both Dan and Stacy have provided better answers.
0 Kudos