add integers to a Numeric field

643
4
Jump to solution
12-21-2022 12:44 PM
dalvarez
New Contributor III

1.           dalvarez_0-1671654945467.png           2.                     dalvarez_1-1671655019435.png

I need all of the values in example one to have 10 integers like the ones one table two. To do So I need to add the necessary amount of ceros before the value.  there are values that need 5, 4, or 1 cero to be added. Any ideas of geoprocessing tools that do this or how to do it by calculating the field ?

Thanks!!!!

 

 

 

same numeric values but the numbers cave ceros in front. 

0 Kudos
1 Solution

Accepted Solutions
JoshuaSharp-Heward
Occasional Contributor III

Hi,

Doing this in field calculator is definitely the best option! Is the AccountID field a text field? Because if it's an integer field you won't be able to add zeros onto the front, and in that case you would need to add a text field to calculate the values into.

As for some code to do what you're trying to do, if you put this in the code block using Python3:

 

def addZeros(ID):
    ID = str(ID)
    noZeros = 10 - len(ID)
    return "0" * noZeros + ID

 

and then the following as the python expression to run above the code block box

 

addZeros(!IDNum!)

 

With the !IDNum! being the Account ID field, that should do what you're after!

Cheers,

Josh

View solution in original post

4 Replies
JoshuaSharp-Heward
Occasional Contributor III

Hi,

Doing this in field calculator is definitely the best option! Is the AccountID field a text field? Because if it's an integer field you won't be able to add zeros onto the front, and in that case you would need to add a text field to calculate the values into.

As for some code to do what you're trying to do, if you put this in the code block using Python3:

 

def addZeros(ID):
    ID = str(ID)
    noZeros = 10 - len(ID)
    return "0" * noZeros + ID

 

and then the following as the python expression to run above the code block box

 

addZeros(!IDNum!)

 

With the !IDNum! being the Account ID field, that should do what you're after!

Cheers,

Josh

dalvarez
New Contributor III

Thanks!!! This worked.

dalvarez_0-1672111440125.png

 

0 Kudos
DanPatterson
MVP Esteemed Contributor
# == example
a = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789]
frmt = "{:010d}"

[frmt.format(i) for i in a]
 
['0000000001',
 '0000000012',
 '0000000123',
 '0000001234',
 '0000012345',
 '0000123456',
 '0001234567',
 '0012345678',
 '0123456789']

# --- field calculator useage

"{:010d}".format(!YourFieldWithNumbers!)

... sort of retired...
KenBuja
MVP Esteemed Contributor

An easy way using Arcade uses Text

 

var num = 1234;
return Text(num,'0000000000');

2022-12-22_7-10-32.png